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/