- Home /
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.
Answer by getyour411 · Feb 15, 2014 at 02:41 AM
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)
}
Answer by Anusha · Feb 13, 2014 at 12:37 PM
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;
}
}
}
Is it only possible to use Raycasting on this scenario? I'm new with Unity so I haven't learned yet on how to do raycast.
No, there are plenty of options. I'm not sure I agree with Anusha's response - what is that On$$anonymous$$ouseDown()ing? Are we saying that's attached to a Cube and we'll need to grow that mess with each new cube - no, that's not good.
Yes it's attached to a cube, and I only needed 3 cubes so I'm not growing it anymore. What are those other options getyour411?
Thanks for the response.
Your variable cubeValue is public, so in the Inspector set it to 1 for cube1, 2 for cube2, 3 for cube3
function On$$anonymous$$ouseDown (){
Debug.Log(cubeValue);
}
Get rid of this
var cube1 : GameObject;
var cube2 : GameObject;
var cube3 : GameObject;
cube1 = GameObject.Find("Cube1");
cube2 = GameObject.Find("Cube2");
cube3 = GameObject.Find("Cube3");
and anything related to Raycast.
$$anonymous$$y dilemma is actually on the if-statement, I can't seem to figure out how to get specific value on 3 different cubes when clicked.
Answer by erick_weil · Feb 15, 2014 at 02:42 PM
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);
}
}
No - this is a repeat of what was posted above. Why are you putting all that junk in here, NONE of it is needed. He's declared public cubeValue. He attaches this script to three different cubes. He goes to each cube and in the inspector changes cubeValue to 1,2 3. THAT'S IT.
public var cubeValue : int;
function On$$anonymous$$ouseDown() {
Debug.Log(cubeValue)
}
Your answer
Follow this Question
Related Questions
.name = " " statement inside OnMouseDown. 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
How to read Shift + Click input? 1 Answer