Hi! I have multiple scenes in my game, as well as background music in my first scene. When I switch scenes, the music stops, but I would like it to continue throughout each scene change, so I was wondering if anyone can tell me how to do that?
Here is the way I do this. In your first scene you could create an entity that persists between scene changes and add a sound component.
let entity = new pc.Entity();
entity.name = 'Persist';
Later you can reference the sound component like this
let sound = app.root.findByName('Persist').sound;
sound.volume = 1.0;
In the dev tools console you can see the entity named “Persist” that was created. It lives outside if the current scene which is the one named “Root”
When I try this, I keep getting this error:
Uncaught TypeError: Cannot read properties of undefined (reading ‘root’)
I guess line 3 of the code above has to be:
I noticed that the changeScene method will “delete all entities and graph nodes under app.root”
So unfortunately that method will also destroy the Persist Entity which is a child of app.root.
I have not noticed that before because I haven’t been using that changeScene method, but my own where only the child named ‘Root’ in app.root is destroyed i.e the current scene. This leaves the Persist Entity in place.
So here is an example project using the Persist entity method described above and using an alternative changeScene method. You can change between scene1 and scene2 using the space bar.