Hi! In this game I’m working on I used math random to spawn enemies from a single template. Each enemy should be hit 5 times before they are destroyed. My problem is that I think they share one counter or life value. So for the first enemy I try to defeat, it work fine, five hits and its destroyed. But for the next few, one hit and they’ll be destroyed. I was wondering how I could fix this
Here are my two codes:
first (connected to the enemy entity):
var MushroomHealth = pc.createScript('mushroomHealth');
MushroomHealth.attributes.add('mushroomLife', {type: 'number', default: 5});
// initialize code called once per entity
MushroomHealth.prototype.initialize = function() {
MushroomHealth.instance = this;
this.gmushroomLife = this.mushroomLife;
};
second (connected to the attack entity):
var BulletCollision = pc.createScript('bulletCollision');
BulletCollision.attributes.add('mushroomEntity1', {type: "entity"})
// initialize code called once per entity
BulletCollision.prototype.initialize = function() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
// update code called every frame
BulletCollision.prototype.onCollisionStart = function(result) {
if (result.other.name == "Mushroom1"){
this.entity.sound.play("hit");
MushroomHealth.instance.gmushroomLife -=1;
if(MushroomHealth.instance.gmushroomLife <=0){
result.other.destroy();
}
}
};