[SOLVED] Scene "Reload" has missing Components

Hello Coders!

I am trying to reload the scene I am currently on. I have followed the example: https://developer.playcanvas.com/en/tutorials/changing-scenes/ which shows how to switch between two different scenes.

Now when doing it with the same scene I get an error.

Uncaught TypeError: Cannot read property 'enabled' of null
    at ElementComponent.get (playcanvas-stable.dbg.js:28767)
    at isEnabledAndHasEnabledElement (playcanvas-stable.dbg.js:38867)
    at Array.filter (<anonymous>)
    at LayoutGroupComponent.reflow (playcanvas-stable.dbg.js:38824)
    at LayoutGroupComponentSystem._processReflowQueue (playcanvas-stable.dbg.js:38982)
    at LayoutGroupComponentSystem._onPostUpdate (playcanvas-stable.dbg.js:38968)
    at Function._helper (playcanvas-stable.dbg.js:28817)
    at Function.postUpdate (playcanvas-stable.dbg.js:28837)
    at Application.update (playcanvas-stable.dbg.js:48855)
    at playcanvas-stable.dbg.js:49460
    at sentryWrapped (helpers.ts:87)

Now this is fixed by destroying the old hirarchy first and then loading the new same one again.

    var oldHierarchy = this.app.root.findByName('Root');
    var scene = this.app.scenes.find(id);
    
    oldHierarchy.destroy();
    this.app.scenes.loadSceneHierarchy(scene.url, function (err, parent) {
        if (err) {
            console.error(err);
        }
    });

BUT it seems, that the hirarchy now is missing components. Some scripts do not have references to the entites that are set via attributes in the editor. Also my Camera does not have a camera component and my SoundManager does not have a sound component anymore.

Any ideas on how to achieve a clean reload of the scene?

Thank you for any help.

Bruno

You will have to destroy the scene first before ‘reloading’ as you cannot load the same scene if it is already loaded due to the GUIDs of the entities.

See: https://github.com/playcanvas/engine/issues/2167

@yaustar Thank you for your reply. Isn’t that what I am doing ? I stated, that first I wasn’t doing it. But in this code example I am using:

 var oldHierarchy = this.app.root.findByName('Root');
    var scene = this.app.scenes.find(id);
    
    oldHierarchy.destroy();
    this.app.scenes.loadSceneHierarchy(scene.url, function (err, parent) {
        if (err) {
            console.error(err);
        }
    });

I do destroy the old hierarchy first. But then when I load the scene. I am missing components for examlple the camera component on my camera.

Or am I missing something else ?

So you are, my bad.

I’ve just done a quick test with that tutorial project and have no issues there: https://playcanvas.com/editor/scene/1008669

Maybe it’s to do with the makeup of the scene? Could you reproduce it in a new project and smallest amount of content?

Okay I found out what the problem was!

I was binding events in my scripts to the application

For Examlpe:

this.app.on("testEvent")....

But the Application does not get destroyed and therefore these events remain. So I had to unbind them all. I did it by just unbinding the intended event before binding it, therefore always removing the event.

1 Like