OnMouseDown If-statement Question

Hi All,
I am new to unity and having problem with the OnMouseDown-If script. I have 3 GameObject (cubes) that I want to return 3 different values. First Cube returns a value of 1, Second Cube returns a value of 2, and Third Cube returns a value of 3.

Here’s what I have so far:

public var cubeValue:int;
var cube1 : GameObject;
var cube2 : GameObject;
var cube3 : GameObject;
cube1 = GameObject.Find("Cube1");
cube2 = GameObject.Find("Cube2");
cube3 = GameObject.Find("Cube3");

function OnMouseDown (){
	if (cube3)
		{
		cubeValue = 1;
		Debug.Log(cubeValue);
 		}
 	if (cube4)
		{
		cubeValue = 2;
		Debug.Log(cubeValue);
		}
	if (cube5)
		{
		cubeValue = 3;
		Debug.Log(cubeValue);
		}
}

I would really appreciate if anyone can help me with this or atleast put me on the correct path. Thank you.

Edit: converted from comment to answer.

There is no IF statement in what I posted, not sure what you are referring to in your last comment.

Keep in mind that attaching the same script to different gameobjects does not mean they must have the same variable values. In other words, attaching your script to the three different cubes and change the public cubeVaue to 1, 2 and 3 - that’s it, no if.

public var cubeValue : int;
 
function OnMouseDown() {
Debug.Log(cubeValue)
}

you need to Raycast inside your OnMouseDown() function, then check which cube …

function OnMouseDown () { 

var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {

if (hit.collider != null)
 { 
switch(hit.collider.name)
{
 case "cube1":cubeValue = 1;
Debug.Log(cubeValue);
break;

case "cube2":cubeValue = 2;
Debug.Log(cubeValue);
break;

case "cube3":cubeValue =3;
Debug.Log(cubeValue);
break;


}
}

}

your " if (cube1)" verify not if tha current cube is the cube1, but if the cube1 exists.
so,

if (cube1) = true, value =1

if (cube2) = true, value =2

if (cube3) = true, value =3

at the end will be debuged “1” “2” “3” all times,
to debug only the cube wich is mouse over do this:

public var cubeValue:int;
var cube1 : String;
var cube2 : String;
var cube3 : String;
cube1 = GameObject.Find("Cube1").name;
cube2 = GameObject.Find("Cube2").name;
cube3 = GameObject.Find("Cube3").name;
 
function OnMouseDown (){
    if (gameObject.name == cube1)
       {
       cubeValue = 1;
       Debug.Log(cubeValue);
      }
    if (gameObject.name == cube2)
       {
       cubeValue = 2;
       Debug.Log(cubeValue);
       }
    if (gameObject.name == cube3)
       {
       cubeValue = 3;
       Debug.Log(cubeValue);
       }
}