Throwing null reference exception for oldHierarchy while reloading the same scene from second time onwards

Scene gets reloaded for the first time. Issue occurs from second time onwards.

Movement.prototype.reloadScene = function() {

    // Get a reference to the scene's root object

    var oldHierarchy = this.app.root.findByName('Root');

   

    if(oldHierarchy)

        oldHierarchy.destroy();

   

    // Get the path to the scene

    var scene = this.app.scenes.find(this.sceneName);

   

    // Remove mouse and touch events

    if (this.app.mouse) this.app.mouse.off();

    if (this.app.touch) this.app.touch.off();

    // Load the scenes entity hierarchy

    this.app.scenes.loadSceneHierarchy(scene.url, function (err, parent) {

        if (!err) {

            console.log('Loaded');

        } else {

            console.error(err);

        }

    });

};

The code generally looks fine although

    if (this.app.mouse) this.app.mouse.off();

    if (this.app.touch) this.app.touch.off();

You shouldn’t do this as it also removes all the event listeners from the systems in the engine such as UI input.

If you can share a project with the issue, that would help a lot.

Also, we recently added documentation about scene loading which may help: Loading Scenes | Learn PlayCanvas