Question by
Tealux · Jul 01, 2019 at 10:38 AM ·
enemy health
How can I make this script to destroy every enemy with this script instead of of only one enemy out of the 50
So I made a script where the enemy have 1HP and when it collides into the player, it will destroy itself since it will take 1 damage. It works good destroying one enemy and that is it, if I duplicate or make another enemy using the same script, it wont destroy the other enemies at all.
public class EnemyOneHealth : MonoBehaviour
{
float maxHealth = 1f;
public static float health;
void Start()
{
health = maxHealth;
}
void OnTriggerEnter2D(Collider2D col)
{
EnemyOneHealth.health -= 1f;
{
if (col.gameObject.tag == "Player")
if (EnemyOneHealth.health == 0f)
{
Destroy(gameObject);
}
}
}
}
Is there any way to make it that it applies to each game object individually?
Comment
Your answer
Follow this Question
Related Questions
Collision killing all enemies not the one the player collides with 2 Answers
Why will my enemy not die? 0 Answers
Targetting Script Errors 0 Answers
Enemy dies in editor mode but not in build 2 Answers
Targeting Script Errors 0 Answers