specfic collisions

I want to make it so that if I collide with a certain game object I will lose health here is my code:

var zombie : Transform;
function OnCollisionEnter() {
if(transform == zombie)
{
hud.hp = hud.hp - 1;
}
}

I get no errors but the hp variable does not go down.

if(transform == zombie)

It’s important to understand what this line does. It says if the transform of the object the script is running on happens to equal whatever object you’ve dragged into the script via the inspector, it will be true.

Since you’ve presumably put this script on your player character (and not a zombie) it means it’ll always return false because your player character isn’t a zombie. Either way, at no point does it test anything against the object you’ve collided with.

Try this instead :

var zombie : Transform; 
function OnCollisionEnter(collision : Collision) { 
if(collision.transform == zombie) 
{ 
hud.hp = hud.hp - 1; 
} 
}

I tried this and still nothing happed, even when I tried putting a debug message in the console, I still got nothing when my objects collided.

Are you making the game object that is suppose to take health a trigger? and if so do you have a collider on your character so that it knows it hit the trigger?

There is no trigger, both my zombie, and my player object have box colliders.

edit: I found out how to fix it, I added a rigid body to my zombie and everything worked!

You could give your Zombies a tag and do something like

if (Collider.gameObject.tag == "Zombie"){

}

I have used that several times and it works.