AR marker detection not working after changing scene twice

I was trying to change scene from something like a main menu into the ar scene, and it worked. Then when I press the back button in ar scene it put me back into the main menu. The problem started when I try to go to the ar scene again. the camera and ui is there, but it cannot detect any marker. I didn’t really change anything from the starter ar project except for the main menu and changing scene button. the script it self has 3 second delay before changing scene so I can put some scene transition to it.
The project: https://playcanvas.com/project/734503/overview/ar-stuff
The script :

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

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

ChangingScenes.prototype.initialize = function(dt) {
    this.change = false;
    this.app.root.findByName("Button").element.on('click', function (event) {
        this.time = 0;
        this.change = true;
    }, this);

};

ChangingScenes.prototype.update = function(dt){
    if(this.change){
        this.time += dt;   
    }

    if(this.time > 3 && this.change){
        this.changeScenes(this.scene1);
        this.change = false;
    } 
};

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

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

var BacktoMenu = pc.createScript('backtoMenu');

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

BacktoMenu.prototype.initialize = function(dt) {
    this.change = false;
    this.app.root.findByName("BackButton").element.on('click', function (event) {
       this.time = 0;
        this.change = true;    
    }, this);

};

BacktoMenu.prototype.update = function(dt){
   if(this.change){
        this.time += dt;   
    }

    if(this.time > 3 && this.change){
        this.changeScenes(this.scene1);
        this.change = false;
    } 
};

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

BacktoMenu.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);
        }
    });
};

Hi, @echako I had the same problem. You might want to try out this code that i used. It’s only a little different than yours. You connect it to both of the scenes.

Hope this helps your project

1 Like

Sorry, @echako I missed this topic. Are you still having trouble with this?