Destroying object with trigger

Trying to destroy with the trigger in the 1st place.
What could be the reason why it does not work in the 2nd place

var TriggerEngelYok = pc.createScript('triggerEngelYok');

TriggerEngelYok.prototype.initialize = function () {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);

};

TriggerEngelYok.prototype.update = function (dt) {

};


TriggerEngelYok.prototype.onTriggerEnter = function (triggerEntity) {
    if (triggerEntity.tags.has('player')) {
        var k = this.app.root.findByName('Root').findByName('Boxengel');
        k.enabled = false;

    }
};

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

Hi @Ozden_Delibas,

The reason that this doesn’t work is because this.app.root.findByName() finds the first entity with the matching name in the entire project and returns it. It doesn’t check for enabled status, and it does not return an array other items with the same name. Only the first one.

You may want to consider script attributes so that you can define a specific object in each instance of your script that should be disabled. Read up on script attributes here:

https://developer.playcanvas.com/en/user-manual/scripting/script-attributes/

2 Likes