Enemy Health Damage

So i have a game where enemy’s spawn and walk towards you and you have to shoot them, only problem is all enemy’s in the scene have there health going back upto 100 when another enemy is instantiated the scripts look like this.

function Start () {
CurrentHealth = FullHealth;
}

function Update () {
Debug.Log(CurrentHealth);

if(CurrentHealth < 0){
CurrentHealth = 0;
}

if(CurrentHealth == 0){
Destroy(gameObject);
}

}
 function DamageHealth(damage : int){
CurrentHealth -= damage;
}

and on the player i have this to damage the zombie’s

var DamageScript: ZombieHealth = GameObject.FindWithTag("Zombie").GetComponent(ZombieHealth);

now i know what the problem is, its because all the zombies when instantiated have the same script and tag but my question is how do i solve this? will i have to have different scripts for each zombie because im planning to instantiate loads at a time.

That’s not the cause of your problem: I suspect you’ve declared CurrentHealth static, what makes it common to all zombies. If so, remove the static keyword - this way there will be a local CurrentHealth for each zombie.

But the way you’re applying damage is wrong too, as you’ve suspected. You must somehow get a reference to the zombie you’ve shot in order to apply damage to the right monster. If you’re shooting with Raycast, get the reference from the RaycastHit structure, like this:

  var hit: RaycastHit;
  if (Physics.Raycast(..., hit)){
    // something was hit - try to get the ZombieHealth script:
    var dmgScript: ZombieHealth = hit.transform.GetComponent(ZombieHealth);
    // if the victim has such script, apply damage 10:
    if (dmgScript) dmgScript.DamageHealth(10);
  }

If you’re hitting the zombie with a projectile, get the reference in OnCollisionEnter (in the bullet script):

function OnCollisionEnter(col: Collision){
  // try to get the ZombieHealth script:
  var dmgScript: ZombieHealth = col.transform.GetComponent(ZombieHealth);
  // if the victim has such script, apply damage 10:
  if (dmgScript) dmgScript.DamageHealth(10);
  Destroy(gameObject); // destroy the bullet
}

bullets need a multiple tagging from different element and different prefab effects from what i experience. i instantiate multiple items on their respective tags so i know what im hitting and this really prevent the triggering objects to activate at real time.