Change scene give errors

Hello, i’m trying to change scene in my project and i used this:

if (targName==='Entrance') {
    app.root.destroy();
    app.loadSceneHierarchy('id_of_project.json', function (err,entity) {
        if (!err) {
            //Level loaded successfully!
        }
    });
}

but don’t work…or better it does but it give lot of errors when load the first scene

found this by @Max:

Never destroy app.root.
Btw, app.root - is not root entity of your scene (I know it is weird, and I’m personally not happy with that too, but it is the way it is).
So all roots of scenes go as children to app.root, so you can destroy them from there.

So i can use the tag for second scene like ‘levels’ and then findByTag(‘levels’).destroy ??

Yep, that would work. Loaded scenes are attached as children to the this.app.root, so you can also list the children of the root and destroy the scene entities there.

if i use the tag just one the ‘Root’ of the secondary scene, that should delete all the children too?
EDIT: tried to use app.root.findByTag(‘levels’).destroy() but says it’s not a function (depressed)

findByTag returns a list

uhm…i see…so i need a little change in code

i have made this since findByTag returns an array:

var dest= app.root.findByTag('levels');
for(var t=0;t<dest.length;t++) {
    dest[t].destroy();
}

it give me the same errors of app.root.destroy, maybe coz i have tagged the root too?
Maybe i need some explanation about root component: i have placed some script in main scene and some different script in levels scene, are the 2 root components different or not?
The error i got is: [player.js:557]: TypeError: this.model.animation is undefined…but if i load the scene…why the animation is undefinded? Have i to keep my player entity?

Please build new simple project with minimum of code and entities, that replicates the problem.
Either that will help you to find your issue, or it will highlight potential bug that we can debug using that example project.

Ok, i will need some time to do that…will post again in some days

If you are tagging the app.root and then destroying it, that will have the same problem as before. You need to not delete the root entity.

Yes i noticed, so i tagged all the entities on the levels scene and deleted them but the animation problem is still there… will try to create a small example but will not be an easy task

Side question, can i check if an animation exist and if not load it?

Get asset, then:

// check if asset resource is not available
if (! asset.resource) {
    // subscribe to loaded event
    asset.once('load', function() {
        console.log('asset loaded');
    });
    // start loading asset
    this.app.assets.load(asset);
}

have created a more simple version of the project here https://playcanvas.com/editor/scene/470471 if u need it more simple let me know…You have to launch levels scene and click on stairs to load the first scene then you get the error

It is very hard to narrow down the issue when there is so much in project.
Best way is to create simple project from scratch that only replicates the problem and no more. We as developers do this all the time to debug issues, but it would be very helpful if users would help us to help them. End of the day we have to prioritise loads of work, and spending time on doing something that you have way more context information to do, might prove challenging for us.

So please, create empty project, and only do things to replicate issue, do not copy-paste code please, re-write only things that are required to narrow down the issue.

OK will try to build it from scratch…

1 Like

Since building it up from scratch is not possible atm (i don’t know where i have saved the player fbx files) so i cleaned all that i could. I have manteined the entity with animations coz it’s that the issue and have removed all others script. Just kept game.js script in Root coz it contains the code for change scene. If this is still too complex i will look for another solution.
PS: i know that simple things are fastest to check and you have lot of work to do, so i don’t blame you at all guys, you are always very helpful, it’s just that movement is the issue so need some file to replicate it
PPS: erased all the unuseful scripts from the priject

Note: This project is using the legacy script system

In a nutshell, in the player.js script, you subscribe the mouse down event which in turn performs a raycast and calls the moveTo function if the raycast hits an object. The problem occurs because you destroy the player entity but don’t unsubscribe from the mouseDown event and therefore the callback to the function still exists but other associated objects have been destroyed (like the model).

So what you need to do to fix this particular problem is to create a destroy function (which gets calls automatically when the component instance is destroyed or removed from the entity) which unsubscribes the mouse down event.

        destroy: function () {
            app.mouse.off("mousedown", this.onMouseDown, this);
        },   

What really helps when you run into issues like this is to open the Developer Tools panel in Chrome and switch to the sources tab as it will stop at the line of code that is causing the exception:

And then you can investigate the callstack to work out what could be causing the issue:

I still think you have some issues with loading levels in general as I get a grey screen after I unsubscribe to the mousedown event but hopefully fixing this will get you part way there.

1 Like

@Steven wow! seems that fixed the problem coz i don’t have anymore the error. Thanks a lot you have made my day brighter

Awesome stuff! :slight_smile:

Look forward to seeing the progress of the project!