"Error loading scripts." when switching scene

Hi, I was using this code to switch to the next scene, this is my second time using it in the project, but the error only happens with this one.

var Himenext2 = pc.createScript('himenext2');
Himenext2.prototype.initialize = function() {
    this.entity.element.on(pc.EVENT_MOUSEUP, this.OpenNextScene, this);
    
};

// update code called every frame
Himenext2.prototype.update = function(dt) {

};

// switch
Himenext2.prototype.OpenNextScene = function(dt) {
    
       this.app.scenes.loadSceneHierarchy(this.app.scenes.find("SecretEnd")); 
       this.app.root.findByName("RootA").destroy();
};

I’m not quite good at coding but what can I do to diagnose the problem? I would also like to note that it does switch to the next scene, and it’s somehow working but the error always pops at the bottom.

Hi @sayhimeee,

I am not sure what’s the cause of the error, though can you try moving the scene destroy() method to the load scene callback? So it happens after the new scene has been loaded:

this.app.scenes.loadSceneHierarchy(this.app.scenes.find("SecretEnd"), function(err){
   if(!err) this.app.root.findByName("RootA").destroy();
}.bind(this)); 

Also, if you boot your app directly on the second scene (SecretEnd), is there no error?

1 Like

Hi @Leonidas,

Yes, when I boot up the SecretEnd scene, there is no error. I have done what you told me to do, and it seems that no error is popping out anymore, so thank you for helping out!

It does seem slower now though, I thought it wasn’t working the first time so I clicked the button again then it loaded the scene twice lol.

Yeah, it seems slower since the old scene is destroyed after the new one has been loaded. So you should handle somehow that delay with e.g. a UI loading indicator.

And also add some logic that doesn’t allow the button to be clicked twice, or execute the scene code two times in a row.

1 Like

Will do! Thanks as always for being so active in the forums :slight_smile:

1 Like