2

Here is my code that attached with the game object that I want to click

void Update () {

    if (Input.GetMouseButtonDown (0)) 
    {                    
            Application.LoadLevel("option");

}

And it seems like I can click anywhere on screen for make "Application.LoadLevel("option");" run.

How can I solve this problem?

Bart
  • 19,692
  • 7
  • 68
  • 77
Goondude
  • 101
  • 1
  • 3
  • 10
  • 2
    That is because you are only checking if the mouse button was clicked not where it was clicked. – Qwertie Feb 10 '14 at 06:07
  • You could use a raycast to check what was under the mouse click. instructions here http://answers.unity3d.com/questions/28064/get-mouse-position-on-object.html – Qwertie Feb 10 '14 at 06:12

2 Answers2

7

As @Qwertie said, where are you clicking is important. If your GameObject has collider attached, then you can use OnMouseDown(). This will be called whenever you click on the gameobject having collider and this script as a component.

void OnMouseDown()
{

     Application.LoadLevel("option");

}
Nick
  • 1,035
  • 1
  • 11
  • 18
0

Try this:

private Vector3 currentMouseDown;
void OnMouseOver ()
{
    if (Input.GetMouseButtonDown (0)) {
        currentMouseDown = Input.mousePosition;
        Debug.Log ("down");

    }
    if (Input.GetMouseButtonUp (0) && currentMouseDown != null) {
        Debug.Log ("Up");
        if (currentMouseDown == Input.mousePosition) {
            Debug.Log ("get");
        }
    }
}

This code will solve drag conflict problem.

Wilson Wu
  • 1,790
  • 19
  • 13