How do you detect collision with 2 objects and change a variable basd on that

I have a script that is
var health=100;

function OnCollisionEnter(collision : Collision)
{
if(collision.gameObject.name == "Cube1")
{
health=health-1;
}
}

where health is the value that I want to change when my player hits the cube. (I changed it when it didn’t work after the original answers)

unitygems.com look at the third topic.

Note than collision.gameObject == "Cube1" won’t works, prefer to use collision.gameObject.name == "Cube1" or the Unity tag system.

You can’t identify a gameObject with a string like “Cube1”. A gameObject is a gameObject, and should normaly just be compared to other gameobjects to see if 2 gameObjects are refering to the same gameObject etc.

You should instead identify it with a name or tag like this:

if(collision.gameObject.name == “Cube1”)
OR
if(collision.gameObject.tag == “Cube1”)

(note that a tag isnt the same as the name of the object)

a late answer but: if it is 2d mode,

it is supposed to be like :

function OnCollisionEnter2D (coll : Collision2D)
{

}