Uncaught TypeError: Cannot read properties of undefined (reading 'root')

Hello, It’s me again. Awhile back ago I needed help with a script and @Leonidas gave me a solution to the script and I am very thankful for that.

But now that same script has a problem that I seem to find. The same error keeps popping up:Uncaught TypeError: Cannot read properties of undefined (reading ‘root’)

So I would really like some help please.

this is the script:

var DeathTeleport2 = pc.createScript('deathTeleport2');


DeathTeleport2.attributes.add("sceneName", {type: "string", default: "", title: "Scene Name to Load"});
// initialize code called once per entity
DeathTeleport2.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', function(event) {
        
        if(other.name('Player') === true){
      // it's the player, do the things you normally do

        
        this.loadScene('Dead');
        }
    }, this);
    


};


DeathTeleport2.prototype.onTriggerEnter = function(entity) {
    entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
    // change the scene
    this.loadScene(this.sceneName); // loadScene function doesn't exist in the script
};



// update code called every frame
DeathTeleport2.prototype.update = function(dt) {
    
};

DeathTeleport2.prototype.loadScene = function (sceneName) {
    // Get a reference to the scene's root object
    var oldHierarchy = this.app.root.findByName ('Root');
    
    // Get the path to the scene
    var scene = this.app.scenes.find(sceneName);
    
    // Load the scenes entity hierarchy
    this.app.scenes.loadSceneHierarchy(scene.url, function (err, parent) {
        if (!err) {
            oldHierarchy.destroy();
        } else {
            console.error(err);
        }
    });
};

// swap method called for script hot-reloading
// inherit your script state here
// StartGame.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/



Do you have a project for people to look at?

Hello @yaustar sorry I haven’t answered you earlier, I was busy with some other things.

I have a project but I don’t plan on announcing it until It is complete and fully built. The project is public though so anyone can see it now.

Hi @DinoCalebProductions! If you share a link of the project someone is be able to help you with the problem.

Ok here is the link.

https://playcanvas.com/editor/scene/1379118

Also the name of the script is called death teleport 2.

I fixed the problem now. thanks for the advice.