Destroy entity destroy also others ones

Hi

I have a project where i clone an existing entity twice in the project.

 var templateCoin10 = pc.app.root.findByName('Coin 10');
    var instanceCoin = templateCoin10.clone();
    instanceCoin.setPosition(new pc.Vec3(0, 0.452, 0));
    pc.app.root.addChild(instanceCoin);
    instanceCoin.enabled = true;
    
    var instanceCoin2 = templateCoin10.clone();
    instanceCoin2.setPosition(new pc.Vec3(0, 0.452, 3));
    pc.app.root.addChild(instanceCoin2);
    instanceCoin2.enabled = true;

The initial entity has a script attached that check collision, and destroy if needed :

var Coin = pc.createScript('coin');

Coin.attributes.add('speed', { type: 'number', default: 30 });

Coin.prototype.initialize = function() {
    console.log('Coin.prototype.initialize');
    this.sound = this.entity.sound;
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

Coin.prototype.update = function(dt) {
    this.entity.rotateLocal(0, this.speed * dt, 0);
};

Coin.prototype.onCollisionStart = function (result) {
    console.log('onCollisionStart: ', result.other.name);
    if (result.other.rigidbody && result.other.name === 'People') {
        this.entity.collision.enabled = false;
        this.entity.children[0].enabled = false;
        this.entity.children[1].particlesystem.play();
        this.sound.play('note');
        setTimeout(() => {
            console.log('destroy: ', this.entity._guid);
            this.entity.destroy();
        }, 1000);        
    }
};

The problem is when the user fire collision with one coin, the others one also disappear…

Hi @vogloblinsky,

Are your coins rigidbodies?
I don’t know if the issue you are experiencing has something to do with it, but if your coins are not rigidbodies, you should probably use “triggerenter” event instead of “collisionstart”.

Other than that, I see no flaws in your code

If the problem persists, could you share the project’s url or make a sample project that replicates this issue?

had a simular issue a while ago, but with a selfmade timeout function. Had solved it by using attributes instead variables…

Ok, so i had the same bad result with triggerenter.

I have created another entity with the same model, and it works fine now.

The only reason i can see, is that my initial entity was used for creating a template, but after that i deleted this template.

I will try to reproduce it later, but seems “may be” to be a bug inside PC.

If you can reproduce the issue, that would be great and we could take a look into it