So, I have a bit of an issue, I’m currently got this “Spawner” script which spawns Instantiate a prefab every second. Each prefab has a “Health” script.
The problem I am facing is, when the Health is at 0, and I call “Destroy(this.gameObject);” it always destroys the objects in the order that they was spawned in. So if I spawn 5, and I shoot the 5th one till its health is 0, it destroys the 1st one spawned.
If I have to provide the scripts I will do. Thank you if you can help me!
Enemy Health Script - Enemy Health Script - Pastebin.com
Spawner Script - http://pastebin.com/qMue6Z9C
If this helps. This is the shoot script!
Shoot Script - http://pastebin.com/agLZVrHS
Try your script like this:
{
//Shared Values
static float health = 100; //Enemys Health
static public bool dead = false; //Is the Enemy dead?
static public void TakeDamage() //
{
health -= 25;
//Debug.Log("Taken damage");
}
void Update()
{
if (health <= 0) // If the enemy health is
{ // 0 then it destroys the
Destroy(gameObject); //Destroys the enemy
}
if (health >= 101) //Makes sure that
{ //Health does not
health = 100; //go above 100
} //so no problems with op zombies
}
void OnTrigger(Collider other) //If it collides with player
{
if (other.gameObject.tag == "Player") //if it does collide with the player
{
//Debug.Log("Hitting Player"); //Tells you in the console
Health.Takeaway(); //Takes player health away through other script
}
}
}