Getting playcanvas error when on scene change

Changing scenes was working fine until today when I was going to make changes.
simple project: https://playcanvas.com/editor/scene/672401

The scene changes automatically back and forth (after 12 sec) without me clicking on the buttons. and I get the following error:[playcanvas-stable.dbg.js:0]: Script error after 10 - 12seconds.

changingScenes script:

var ChangingScenes = pc.createScript('changingScenes');

ChangingScenes.attributes.add("sceneId", {type: "string", default: "0", title: "Scene ID to Load"});

ChangingScenes.prototype.initialize = function(dt) {
    // Change scenes in 1 second
    var self = this;
    setTimeout(function (){ 
        self.changeScenes();
    }, 15000);
};

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 ();
    });
};

ChangingScenes.prototype.loadScene = function (id, callback) {
    // Get the path to the scene
    var url = id  + ".json";
    
    // Load the scenes entity hierarchy
    this.app.loadSceneHierarchy(url, function (err, parent) {
        if (!err) {
            callback(parent);
        } else {
            console.error (err);
        }
    });
};

It only happens when you click the button to change scene and then wait. If I wait in both scenes, it works fine. Did you cancel the setTimeout in ChangingScenes script when you press the button to load the scene? If not, that can cause very odd behaviour.

Side note: Bear in mind that each time you load the video scene, you are adding a new video DOM object to the page.

This fixed the issue
Thanks

It might be useful to use the relatively new getSceneUrl(sceneName) function, in case you fork the project later on.

Change…

ChangingScenes.attributes.add("sceneId", {type: "string", default: "0", title: "Scene ID to Load"});

to…

ChangingScenes.attributes.add("sceneName", {type: "string", default: "", title: "Scene Name to Load"});

( Don’t forget to change the value in the editor from the Scene ID number to the actual name of the scene )

and…

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

to…

var sceneId = this.app.getSceneUrl(this.sceneName);
this.loadScene (sceneId, function () {
         // Once the new scene has been loaded, destroy the old one
         oldHierarchy.destroy ();
});
1 Like