I am trying to use the loadSceneHierarchy on my project to do the main menu scene to game scene transition, I am trying to do this with this code:

        app.loadSceneHierarchy("423442.json", function (err, parent) {
            if( err ){
                console.log("no erros loading level");

This is loading the scene with no errors, but I am getting an error after a mouse click on the next scene.

I am getting the error below when loading the 423442 scene from 426718:

[MouseClick.js:76]: Uncaught TypeError: Cannot read property 'screenToWorld' of undefined

TypeError: Cannot read property 'screenToWorld' of undefined
    at Object.MouseClick.onSelect (http://playcanvas.com/api/files/code/391312/directory/MouseClick.js:76:46)
    at Object.Events.fire (https://code.playcanvas.com/playcanvas-stable.js:640:37)
    at Object.Mouse._handleDown (https://code.playcanvas.com/playcanvas-stable.js:14180:10)

Any idea?

Best Regards,

I just found the root problem. I am getting this error because the script that loads the game scene from the menu scene is NOT being destroyed and is being executed by my mouse click on scene 2. Is there any way to deactivate/destroy/unload the LoadScene script after its execution? Is there any pc.script.destroy function or something like this?

I just found a solution, and its [here][1]
[1]: https://playcanvas.com/editor/code/391312/LoadScene.js

I did a disableEvents function that detachs the mouse/touch events from the script… The destroy method should do it for us :wink:

PS: is it the definitive solution? Is the any way to destroy all event listeners?

1 Like

Yeah, the thing is that while you destroy the script, the app.on (‘event’, this.callback, this) is not destroyed because it was called on the app - app.on () - not on your script. BUT supplied callback is the part of your script! So when the script gets destroyed, you try to call already non-existing callback by still existing mouse / touch event and get an error.

Every script has destroy () function where you should do cleanups, like app.off () and all other stuff.
See there: http://developer.playcanvas.com/en/user-manual/scripting/anatomy/

1 Like