Script for enemy health loss on contact with bullet prefab

var enemyhealth : float = 30.0;

function OnTriggerEnter (hit : Collider)
{ 
   
     if(hit.transform.gameObject.name == "bullet")
     {    
          Debug.Log("hit!");
          Healthloss();
     }
}

function Healthloss()
{

var othergameobject = GameObject.Find("bullet");
var otherscript = othergameobject.GetComponent(bulletdestroyscript); 


     enemyhealth-=otherscript.Damage;
         
     if (enemyhealth<0)
     {
          Destroy (gameObject);
     }
}

This is the code that I have.

I have attached it to the enemy object and it just doesn’t seem to do anything.

Could anyone help me out?

Cheers.

Hmmm… unless you only have one bullet, I would guess that the main issue is that the clones of the bullet prefab aren’t the same thing as the “bullet” you are trying to find. Each one is it’s own separate bullet(clone) I think.

Try giving the bullet prefab a tag, and checking for that, rather than trying to check the name. That way anything with that tag will trigger the function.

Also, I would suggest doing hit.getcomponent to get the bullet’s script- that way you don’t have to worry about finding the right bullet!

Hope that gets that enemy dead! :slight_smile: