Understanding root cause:
You define local variable health
. It’s simply same for all script instances. If you modify it for one enemy it is modified for the others. self
is on the other hand unique for all instances. Read more:
Explanation of behavior visible in the video:
But how to exaplain the behavior from the video?
You can say “it would then be that the enemy behind should update the health too each time?”
No, because you are calling damage
only in on_message and it looks like you are sending the message to the correct game object correctly. So that only first object receives a message “damage
” and it sets a new health
value in label, so you see it only on the first enemy.
But, as explained above, you are modifying health
for all instances, so that when the first enemy is deleted correctly after health < 0
, you have a situation you are shooting the second enemy and it gets message damage
, so calls damage
function, but health is already < 0!
So it instantly sets label to updated health value (health - value, probably -2) and because it’s <= 0 removes instantly the second enemy
Advices:
- Read the chapter on scoping from the link above
- Use extensively
print()
if in trouble
- or use Defold debugging (which would be soo cool in such a case, put a breakpoint in the beginning of damage function and see what health values you have, when the second enemy is killed)
Let us know if it solves the issue and if you need any help (jeez, sounds like AI xD)