How do I switch between scenes?


I have this code, but it doesn’t work
image

HI @sAp,

Check out this prebuilt Scene Manager:

https://playcanvas.com/project/756176/overview/scene-manager

The problem with this script appears to be that this.sceneId does not appear to be defined in your changeScenes() function.

I hope the project above can help you. Feel free to fork it and use it in your own projects.

2 Likes

I just tried to ctrl+c and ctrl+v your code in my own project to check, whether it works. And now when I click the button, nothing happens. No errors, no actions
P.S. I attached script “button.js” to the button

Hi @sAp,

If you take a closer look at the project, you will see that there is a bit more setup to it. The Scene Manager relies on having a scene called ‘Init’ with the SceneManager entity. The SceneManager entity has the sceneManager script attached to it and loads the first scene you tell it to. After that you can fire the ‘changeScene’ event from anywhere in your project to change the scene. Since you want to use the button.js script you can attach it to your button and if you are loading the Init scene first it will attempt to change to the scene with the name your provide in the editor attribute.

Since you copied and pasted the scripts, make sure you click the parse button on them to populate the editor fields.

In short:

  • Create an Init scene
  • Create a SceneManager entity with the scenceManager script attached and parsed
  • Populate the startScene attribute in the editor
  • Attach button.js to your button and parse it
  • Populate the targetScene attribute in the editor with the name of your targetScene
  • Launch the project from the Init scene
  • Click the button to change scenes.
1 Like

Thank u! It works!

1 Like

Now I bumped into a problem:)
I have an html code and script that allows to display/hide the html block. When I change the scene to the one, which has this html code, it displays this html block instantly, without any actions which I don’t want to happen. Can u please, tell me, where is the problem?

I solved the problem but still don’t know why it happened )