- Home /
Multiple Enemies dying rather than one?!
Using this code, I get multiple enemies dying at once rather than a single enemy
using UnityEngine; using System.Collections;
public class EnemyHealth : MonoBehaviour { public int startingHealth = 100;
public int currentHealth;
public bool isDead;
void Awake ()
{
currentHealth = startingHealth;
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == "PistolBullet")
{
currentHealth = startingHealth - 10;
}
if (currentHealth <=0)
{
isDead = true;
Dead ();
}
}
void Dead ()
{
if (isDead = true)
{
Destroy (gameObject, 2f);
}
}
}
Comment
I don't see anything wrong here. Do you have any other scripts attached to these enemies? Do you have any other scripts that reference these enemies?
Answer by JC_SummitTech · Mar 31, 2017 at 08:15 PM
You may have forgotten to ensure a bullet can only hit once (rather than going through several enemies) but this code seems ok.