Same prefab enemys .... different health var

Hi … when i kill one enemy he die … but when i go to kill the other one nothing happning … the enemy health still go down ;
how to make one var to be for all enemys

enemy script

function TakeDammage( Damage:int){

health -= Damage;
if( health<=0){

enemy1.animation.Play(“dead1”);
}

}

player Script

function Shot(){

yield WaitForSeconds (0.02);
var fwd = m16Front.TransformDirection (Vector3(0,-1,0));
if ( Physics.Raycast( m16Front.position, fwd, hit)){

if( hit.collider.tag==“enemy”){
enemys.gameObject.GetComponent(“enemy”).TakeDammage( damage);

}

}
}

Yep, that’s was the case. Just print

if(hit.collider.tag=="enemy")
    hit.transform.gameObject.GetComponent<Enemy>().TakeDammage(damage);

that’ll damage exact enemy you had hit.

Or… as long as you have collection of all enemiues you can do something like this

enemies.Single(e => e.GetInstanceID() == hit.transform.gameObject.GetInstanceID()).TakeDammage(damage);

but my first sample won’t do the huge perfomance hit, unless you constantly hitting enemies several times in a second :slight_smile:


oh, I just realized, enemies is probably a parent GameObject for enemies, not the enemies collection, so enemies.Single() won’t work. You’ll better just stick to first sample, it should work fine