How to destroy the whole scene

Friends, I want to destroy the whole scene in if. I want it to destroy the whole scene when the ‘‘engel’’ hits its object, but it doesn’t work, why?

    var parkuryok=this.app.root.findByName("");
      if(result.other.tags.has('engel')){
        parkuryok.destroy();
        this.loadScene(this.sceneId,function(){
        });
      }

Hi @Ozden_Delibas! I think you have to change your first line.

var parkuryok = this.app.root.findByName('Root');

Check the tutorial below how to change a scene.

https://developer.playcanvas.com/en/tutorials/changing-scenes/

1 Like

I don’t understand why friends don’t delete the scene during menu transitions, can you help me?

var SahneDegisim = pc.createScript('sahneDegisim');

SahneDegisim.attributes.add("sceneId", {type: "string", default: "0", title: "Scene ID to Load"});

//SahneDegisim.attributes.add('controllerEntity', { type: 'entity' });

// initialize code called once per entity
SahneDegisim.prototype.initialize = function() 
{
    //this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.entity.element.on("mouseente",this.onEnter,this);
    this.entity.element.on("mousedown",this.onPress,this);
    this.entity.element.on("mouseup",this.onRelease,this);
    this.entity.element.on("mouseleave",this.onLeave,this);
    
    this.entity.element.on("touchstart",this.onPress,this);
    this.entity.element.on("touchend",this.onRelease,this);
};


SahneDegisim.prototype.onPress = function(event) {
    var oldHierarchy = this.app.root.findByName('Root'); 
    oldHierarchy.destroy();
    this.loadScene(this.sceneId,function(){
    });
};


SahneDegisim.prototype.onCollisionStart = function(){
};

SahneDegisim.prototype.loadScene = function(id,callback) {
    var url=id+".json";
    this.app.loadSceneHierarchy(url,function(err,parent){
        if(!err)
            callback(parent);
        else 
            console.error(err);
    });
};

Hello @Ozden_Delibas! Did you try to debug your onPress function? You can use the console of your browser for that. I suggest to start with the console.log below.

SahneDegisim.prototype.onPress = function(event) {
    var oldHierarchy = this.app.root.findByName('Root'); 
    console.log(oldHierarchy);
    oldHierarchy.destroy();
    this.loadScene(this.sceneId,function(){
    });
};
1 Like

So how should I check?

To open the console in Chrome, use the keyboard shortcut Cmd + Option + J on a Mac or Ctrl +Shift +J on Windows. As an alternative, you can right-click on the webpage and click “Inspect” to open the developer console. Click the “Console” tab in that window.

1 Like

i get a screen like this

Yes, that’s correct. The last part (after the warnings), is the result of your console.log. You can see it has find an entity with the name ‘Root’, so that part of your script works as expected.

1 Like

So why aren’t the scenes transitioning?

That way you have to debug all steps of your script to find out where it’s going wrong. If you share a link to the editor of your project I can take a look later today.

1 Like

https://playcanvas.com/editor/scene/1259384

@Ozden_Delibas I think our friend means that, while random paths are created with the “.clone()” function in Scene-1, it means that the cloned objects do not disappear when the ball hits an obstacle on the road or falls to the ground. How do we destroy cloned objects? He asks what is the reason for this.

@Albertos thank you for your answers.

With the cloned objects, are you adding them to the scene root or the app root?

Adding them to app.root adds it to the application root node but the scene root node is a child of the app.root.

So when the scene root node is destroyed, the clone entities that get added to the app.root don’t get destroyed because they aren’t a child of the scene root node.

TLDR, instead of app.root.addChildfor clones, try this.app.root.findByName('Root').addChild

2 Likes

I tested it in the sample project. Now it started working correctly. Thank you.