[SOLVED] Loading a Scene from another - no initialize function called

Hey all -

I’m trying to load a scene from another scene. I’m using the following logic:

    this.entity.button.on('click', (e) => {

        var oldHierarchy = this.app.root.findByName('Game');

        // What happens when this button is clicked?
        this.app.scenes.loadScene("13280.json", () => {
            oldHierarchy.enabled = false;
    }, this);

The new scene loads, and the old one does indeed disappear, but none of the initialize functions are being fired on the newly loaded scene. For example, on the root object of my new scene, I have attached a script file. In that file, there’s a console.log in initalize that never gets called; yet the console.log that I have in update does get called.

What am I missing?

Thanks much-

Hi, by looking at your logic, I cannot tell where the issue could be, If you can post a repro of it in a project, I can suggest you better.

Did a quick test with a scene having a script in its root and changing to the next scene, and it correctly shows the initialize log:

1 Like

Don’t use loadScene. It’s technically an internal function that is is exposed for engine only users https://developer.playcanvas.com/api/pc.SceneRegistry.html#loadScene

Use https://developer.playcanvas.com/api/pc.SceneRegistry.html#loadSceneHierarchy and https://developer.playcanvas.com/api/pc.SceneRegistry.html#loadSceneSettings instead

Examples of usage: https://developer.playcanvas.com/en/tutorials/?tags=scenes

Thank you. It looks like I only need to use the loadSceneHierarchy function since both my scenes use the same settings, so loading settings is redundant.