- Home /
.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.
Answer by Dave-Carlile · Feb 27, 2013 at 01:20 PM
"=" 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.
Sorry that was a typo, I know the == sign but I can not assign separate objects within On$$anonymous$$ouseDown so within one function if I click on a blue box it moves it 10 up and if I click on a red box it moves it 10 to the right. i.e.
var BlueBox: GameObject;
var RedBox: GameObject;
On$$anonymous$$ouseDown () {
if (gameObject.name=="BlueBox") {
transform.position=Vector3(0,10,0); }
if (gameObject.name=="RedBox") {
transform.position=Vector3(10,0,0); }
}
Sorry again as I realise that is also kind of misleading. That script would be attached to my main character and would be able to control the boxes from there.
Basically what I am trying to achieve is a player trying to climb a ladder so that the script is attached to my main character and when he clicks on the rungs of the ladder he lerps upwards. (used a shorter script above for ease of reading/typing quickly)
EDIT: and using "if" statement in this way does not achieve this.
+thanks for the help.
Oh, I think I see. You want to do something different depending on which box is clicked? I would suggest adding a collider to the boxes, and in On$$anonymous$$ouseDown do a ray cast to deter$$anonymous$$e which box was clicked.
http://answers.unity3d.com/questions/13577/using-raycast-to-get-mouse-input.html
Ah! Why did I not think of this and have wasted hours trying to do it the other way. I Should have been using raycasts from the beginning. Cheers Dave.
Answer by Bunny83 · Feb 27, 2013 at 04:36 PM
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;
}
Hey thanks for the answer, another answer suggested raycasts and I realised that's what I should have been thinking about from the beginning. Thank you though. $$anonymous$$ike.
Actually, @Bunny83's answer is probably the better one. I didn't realize On$$anonymous$$ouseDown happened in response to clicking on a collider - thought it was just a normal "mouse was clicked" thing.
@ELBANDID0: Well, the On$$anonymous$$ouseXXXX events are also using a raycast ;) Unity automatically casts a ray at the mouse position and sends messages to the collider it hits. Note that GUIElements, which can't be hit by a raycast since they are gui objects, also produce those events when clicked / hovered. Also watch out, in the past Unity didn't send these events on mobile devices. However it seems they changed this behaviour but i wouldn't blindly trust on that ;)
Thanks for all the suggestions fellas. Currently I am sticking with the raycast method (probably because I am lazy and have already set it up) and it works fine for what I am trying to achieve. I will probably be implementing this method soon though.
Thanks for the help again.
Answer by MickM · Feb 27, 2013 at 01:25 PM
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
Your answer
Follow this Question
Related Questions
OnMouseDown If-statement Question 3 Answers
change variable name inside IF statement, or add text to it 3 Answers
Object doesn't move 2 Answers
Tips on how to handle multiple if statemens 1 Answer
Disable Mouse Input when OnGUI() 1 Answer