[SOLVED] How to change scenes using interface buttons

Ok, this is a really stupid mistake on my part. Change your function call to this.changeScenes(), with the s at the end. Guess this is what happens when I try to type code on mobile😂. Edited my original post as well.

EDIT -

Just so you can troubleshoot this issue on your own next time, when you get an error message saying function is not defined, then just scroll through your list of functions in the script, and double check your call. More often than not, you’d be able to fix it yourself.

Yes, there was this mistake…

very interesting situation, right now it showing me that its “not found”, but I inserted id numbers of scenes, and parsed codes… :unamused:

Can you double check the numbers?

https://playcanvas.com/editor/scene/916873
https://playcanvas.com/editor/scene/916876
https://playcanvas.com/editor/scene/916970
image

Hello! Is there also a console error?

Hello,

I don`t see console mistakes…=(

Do you already know what exactly is ‘not found’?

I think it cant find the scenes, because I see this report after clicking on buttons, but I`m not sure exactly…

Do you have a project link?

Great news, I will try to reboot my laptop…

Sorry it was the example project. Do you have a project link? Maybe I can see a mistake…

Yes, ofcourse:

https://playcanvas.com/editor/scene/916895

Pls let me know what do you think about it, I`m very new in it.

I also don’t have a lot of experience, but I can always help to find out where things go wrong.

Are you sure this code is oke:

    this.loadScene (this.sceneId, function () {
        // Once the new scene has been loaded, destroy the old one
        oldHierarchy.destroy ();
    });

I can’t find where you set this.sceneId.

Try to change this:

ChangingScenes.prototype.changeScenes = function() {
    // Get a reference to the current root object
    var oldHierarchy = this.app.root.findByName ('Root');
    
    // Load the new scene. The scene ID is found by loading the scene in the editor and 
    // taking the number from the URL
    // e.g. If the URL when Scene 1 is loaded is: https://playcanvas.com/editor/scene/475211
    // The ID is the number on the end (475211)
    this.loadScene (this.sceneId, function () {
        // Once the new scene has been loaded, destroy the old one
        oldHierarchy.destroy ();
    });
};

To this:

ChangingScenes.prototype.changeScenes = function(sceneId) {
    // Get a reference to the current root object
    var oldHierarchy = this.app.root.findByName ('Root');
    
    // Load the new scene. The scene ID is found by loading the scene in the editor and 
    // taking the number from the URL
    // e.g. If the URL when Scene 1 is loaded is: https://playcanvas.com/editor/scene/475211
    // The ID is the number on the end (475211)
    this.loadScene (sceneId, function () {
        // Once the new scene has been loaded, destroy the old one
        oldHierarchy.destroy ();
    });
};

If you add me to the project I can help better.

1 Like

Yes, you helped me, it works :ok_hand: :+1:

But now I figured out that I don`t have abilities to come back to my first page after chosing one scene among those three that I have… :crazy_face:

Great!

You need a button or something on every scene to go back to the ‘scene selection’ scene. I don’t know what kind of game you want to make, so I don’t know what is the best way in your case.

1 Like

Yes, because of your help I have base, I have some ideas of how to use that to finish my first step.

Thank you, now I have what to study!!!

1 Like

Sorry about the delay in my response @Ivan_Stratiichuk. My idea would be to have a selection scene in your game, that leads to every other scene. Then, in all the other scenes, you can have a back to selection button. That’s what I normally do in my games.

1 Like

Yep, I will add back button folowing the same logic of changing scene code, I will add this code to each scene but all this scenes will gave the same ID number of source scene, so it will back me to scene from which I started… :+1: :ok_hand: :sunglasses:

2 Likes

Hi @Leonidas I am trying to create a PlayCanvas scene that switches between two senes using a button and am trying to open the scene you posted as an example:

https://playcanvas.com/editor/scene/916857

However, it is not opening. Thanks ahead for any help.

Hi @Ian_Curtis,

The way I set this up is by having a scene manager that transports itself between scenes. To ensure that it only gets loaded once, I made an initialization scene that loads the first scene of my choosing and then uses app events fired by the buttons to change scenes from there.

Check out :

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

To see it at work.

2 Likes