The code does not work for teleport

i keep getting this eror that says Uncaught TypeError: Cannot read property ‘teleportable’ of undefined heres the code

var Teleport = pc.createScript('teleport');

Teleport.attributes.add('target', {
    type: 'entity',
    title: 'Target Entity',
    description: 'The target entity where we are going to teleport'
});

// initialize code called once per entity
Teleport.prototype.initialize = function() {
    if (this.target) {
        // Subscribe to the triggerenter event of this entity's collision component.
        // This will be fired when a rigid body enters this collision volume.
        this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    }
};

Teleport.prototype.onTriggerEnter = function (otherEntity) {
    // it is not teleportable
    if (! otherEntity.script.teleportable)
        return;

    // teleport entity to the target entity
    otherEntity.script.teleportable.teleport(this.entity, this.target);

heres the map https://launch.playcanvas.com/947289?debug=true

heres the map https://launch.playcanvas.com/947289?debug=true
heres the code
var Teleport = pc.createScript(‘teleport’);

Teleport.attributes.add(‘target’, {
type: ‘entity’,
title: ‘Target Entity’,
description: ‘The target entity where we are going to teleport’
});

// initialize code called once per entity
Teleport.prototype.initialize = function() {
if (this.target) {
// Subscribe to the triggerenter event of this entity’s collision component.
// This will be fired when a rigid body enters this collision volume.
this.entity.collision.on(‘triggerenter’, this.onTriggerEnter, this);
}
};

Teleport.prototype.onTriggerEnter = function (otherEntity) {
// it is not teleportable
if (! otherEntity.script.teleportable)
return;

// teleport entity to the target entity
otherEntity.script.teleportable.teleport(this.entity, this.target);

};

Hello @max_and_kenn! If you want to add your code here on the forum, please do that with the Code Highlight and place all the code between the signs.

image

Otherwise it’s very difficult to read your post.

ok heres the code `var Teleport = pc.createScript(‘teleport’);

Teleport.attributes.add(‘target’, {
type: ‘entity’,
title: ‘Target Entity’,
description: ‘The target entity where we are going to teleport’
});

// initialize code called once per entity
Teleport.prototype.initialize = function() {
if (this.target) {
// Subscribe to the triggerenter event of this entity’s collision component.
// This will be fired when a rigid body enters this collision volume.
this.entity.collision.on(‘triggerenter’, this.onTriggerEnter, this);
}
};

Teleport.prototype.onTriggerEnter = function (otherEntity) {
// it is not teleportable
if (! otherEntity.script.teleportable)
return;

// teleport entity to the target entity
otherEntity.script.teleportable.teleport(this.entity, this.target);

};
`

Please add your code in the way I just showed you. (You can correct your current post with the edit option).

ok code

var Teleport = pc.createScript('teleport');

Teleport.attributes.add('target', {
    type: 'entity',
    title: 'Target Entity',
    description: 'The target entity where we are going to teleport'
});

// initialize code called once per entity
Teleport.prototype.initialize = function() {
    if (this.target) {
        // Subscribe to the triggerenter event of this entity's collision component.
        // This will be fired when a rigid body enters this collision volume.
        this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    }
};

Teleport.prototype.onTriggerEnter = function (otherEntity) {
    // it is not teleportable
    if (! otherEntity.script.teleportable)
        return;

    // teleport entity to the target entity
    otherEntity.script.teleportable.teleport(this.entity, this.target);
};

Perfect! Thank you! Which part is not working?

if you go on the game it shows you
game https://launch.playcanvas.com/947289?debug=true

Try to change this to:

if (otherEntity && !otherEntity.script.teleportable)

But I’m also not sure if !otherEntity.script.teleportable is a correct check.

could you put it in the code i dont know were to put it

It’s inside your onTriggerEnter function. First you want to check if there is an “otherEntity”. Ater that you can check if that “otherEntity” has that script or not.

i dont know what that means sorry i just jioned 3 days ago

You can try this:


var Teleport = pc.createScript('teleport');

Teleport.attributes.add('target', {
    type: 'entity',
    title: 'Target Entity',
    description: 'The target entity where we are going to teleport'
});

// initialize code called once per entity
Teleport.prototype.initialize = function() {
    if (this.target) {
        // Subscribe to the triggerenter event of this entity's collision component.
        // This will be fired when a rigid body enters this collision volume.
        this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    }
};

Teleport.prototype.onTriggerEnter = function (otherEntity) {
    // it is not teleportable
    if (otherEntity && !otherEntity.script.teleportable)
        return;

    // teleport entity to the target entity
    otherEntity.script.teleportable.teleport(this.entity, this.target);
};

it still doesnt work

I’m not on a computer at the moment so it’s hard to see what’s wrong. Maybe it’s something with your setup. Is the error on the same part of the code?

Uncaught TypeError: Cannot read property ‘teleportable’ of undefined

Then you also have to check if the entity has a script component.

Try this:


var Teleport = pc.createScript('teleport');

Teleport.attributes.add('target', {
    type: 'entity',
    title: 'Target Entity',
    description: 'The target entity where we are going to teleport'
});

// initialize code called once per entity
Teleport.prototype.initialize = function() {
    if (this.target) {
        // Subscribe to the triggerenter event of this entity's collision component.
        // This will be fired when a rigid body enters this collision volume.
        this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    }
};

Teleport.prototype.onTriggerEnter = function (otherEntity) {
    // it is not teleportable
    if (otherEntity && otherEntity.script !== undefined && !otherEntity.script.teleportable)
        return;

    // teleport entity to the target entity
    otherEntity.script.teleportable.teleport(this.entity, this.target);
};

Please don’t forget to save your code before testing!

wut how do i do that

If you use a windows computer you can use CTRL + S.