Swap scene error (with persistent entity)

I’m trying to create a persistent Entity (that doesn’t be destroyed in the chage of scenes).
You can find the project here: https://playcanvas.com/project/660089/ and debug the error.

Basically in the MainMenu scene there is the Loader entity that must persist during the change scene to the GamePlay one.

I’m trying to make a Singleton instance of this entity too.

From the main menu you can swap to the gameplay scene pressing the red button. The flow is this one:

  1. One you pressed the red button, the Loader load all the assets of the scene that you want to load and activate the LoadingScreen (a child of the Loader entity).
  2. Once the assets loading is complete, the Loader start loading the new scene.
  3. Once the Scene is loaded swap the parent of the Loader from the root of the old scene to the root of the new scene.
  4. Destroy the root of the old scene.
  5. Disable the LoadingScreen entity.

At the first swapping of scene, the process is completed without errors. Once you try to swap the scene again (pressing the ESC buttom from the gameplay scene), an error is raised when you try to achieve the 5th step (disable the LoadingScreen). It seems that a double change of parent node break something when you try to enable/disable an entity.
This behaviour happen from the second change of sceen. If you copy the Loader entity in the GamePlayScene and you start the project from that Scene, the first swap of scene is ok (from GamePlay to MainMenu) but the second one (form MainMenu to GamePlay) fails again in the same way described.

Someone already faced a problem like this?


Hi @Dave_C,

Your scene swapping code seems correct from a quick glance, not sure what is wrong. The Playcanvas loadSceneHierarchy is perfect for scenarios where you want to swap only part of the hierarchy and keep one or more entities in place (singleton).

Do you unload any assets at any point? I got a material missing error after trying to execute the 2nd swap.