.name = " " statement inside OnMouseDown.

Hi,

I would like to know (if possible) how to place various if statements inside OnMouseDown i.e.

OnMouseDown () {

if (gameObject.name=“tube1”) {

 //move upwards

}

if (gameObject.name=“tube2”) {

 //move downwards

}

}

Thanks in advance.

“=” is the assignment operator. “==” is the equality operator. So when comparing the name you need to use “==”…

if (gameObject.name == "tube1")
{
}

Otherwise, not completely sure what you’re asking for.

In your case you should just make a public variable so you can assign the direction in the inspector:

// UnityScript

var direction : Vector3 = Vector3(10,0,0);

function OnMouseDown ()
{
    transform.position += direction;
}

Just attach this to each of your boxes and set the direction the way you like in the inspector.

To be more flexible you could also add a Transform reference which will be moved. That way you can seperate the “trigger” from the affected object:

// UnityScript

var target : Transform;    
var direction : Vector3 = Vector3(10,0,0);

function OnMouseDown ()
{
    if (target == null)
        target = transform; // if no target assigned, use the own transform
    target.position += direction;
}

Yes you can place multiple if statements in any function!

Also, and importantly, as above, you need to use == when comparing values
You will get an error with that code and it won’t compile because you are effectively assigning values instead of providing a condition check