Basic Enemy Ai Health/Spawn problem

I know this is a 2 in 1 topic but i cant figure it out by my own.
Editor: PlayCanvas | HTML5 Game Engine
I got the editor link above, so my problems are

  1. the enemies share the same hp pool on the healthbar of the enemy is the script where the enemy can recieve damage i trigger this event onCollision but how can i ensure that it only trigger on the enemy actually “hit”?

  2. If i activate my enemy spawner the first one spawning is working like intended but after the next spawn cycle i get a error that the progressImage (the part of the healthbar im manipulating to indicate enemy health) is undefined but why?.. i made it a attribute and dragged the image onto it so if make a prefab out of the enemy it should work right? or do i have to asign it via code?

Hi @IlAce!

If you want to use events, you maybe can do something like below.

this.app.fire('enemyHit', this.damage, this.entity);
EnemyHealthBar.prototype.setProgress = function (value, entity) {
    if (entity === this.entity.parent) {
        // your code
    }
};

It would be nice if it were possible to only fire an event at the entity, but that doesn’t seem possible.

this.entity.fire('enemyHit', this.damage);

Alternatively you call a function of another script directly.

scriptEntity.script.scriptName.functionName();

I didn’t see this error, probably because no new enemies are spawned.