[SOLVED] Destroy object is destroying all of my objects

After some trying a few different ways, I got my scripts working correctly except for one major issue.

I have my scripts set up to delete an object using the E key when the player enters the trigger area, but not before. Unfortunately, this script is also deleting all other objects with that include a trigger area.

This is the script I am using at the moment.

var Mining = pc.createScript('mining');

var Trigger = false;

// initialize code called once per entity
Mining.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
};

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

Mining.prototype.onTriggerEnter = function(entity) {
    Trigger = true;
};

Mining.prototype.onKeyDown = function(event) {
    if(event.key === pc.KEY_E && Trigger) {
        this.entity.children[0].destroy();
    }
};

https://playcanvas.com/project/629140/overview/thesis-visual-concept

You are using a global variable instead of a class/instance scoped one. So all the triggers are checking against the same variable when you press E.

1 Like

I changed it to a local variable, but it still seems to be causing the same problem.

var Mining = pc.createScript('mining');

// initialize code called once per entity
Mining.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
};

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

Mining.prototype.onTriggerEnter = function(entity) {
    
};

Mining.prototype.onKeyDown = function(event) {
    var Trigger = false;
    if(this.onTriggerEnter) {
        Trigger = true;
    }
    if(event.key === pc.KEY_E && Trigger) {
        this.entity.children[0].destroy();
    }
};

You are on the right track but you are using a local variable instead of class scoped one.

Also

    if(this.onTriggerEnter) {
        Trigger = true;
    }

Is checking if the function onTriggerEnter exists (which it does), so the local variable Trigger is always true.

The following code is creating a class scoped variable that is set to true/false when something enters the trigger volume:

var ChopDown = pc.createScript('chopDown');

// initialize code called once per entity
ChopDown.prototype.initialize = function() {
    this.inTrigger = false;
    
    
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    this.entity.collision.on('triggerleave', this.onTriggerLeave, this);
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
};


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


ChopDown.prototype.onTriggerEnter = function(entity) {
    this.inTrigger = true;
};



ChopDown.prototype.onTriggerLeave = function(entity) {
    this.inTrigger = false;
};


ChopDown.prototype.onKeyDown = function(event) {
    if(event.key === pc.KEY_E && this.inTrigger) {
        // Remove all the listeners
        this.entity.collision.off('triggerenter', this.onTriggerEnter, this);
        this.entity.collision.off('triggerleave', this.onTriggerLeave, this);
        this.app.keyboard.off(pc.EVENT_KEYDOWN, this.onKeyDown, this);
        
        this.entity.children[0].destroy();
    }
};
1 Like

That did it. I was unfamiliar with class scoped variables. Thank you very much!