Version: Unity3D 4.6.2f1

I have a grid of tiles (Cube GameObjects with a Tile script attached). They are positioned at their index location [x][y] = (x, y, z=0) World Coordinates.

Below, I try to get the coordinates of the tile upon which the mouse left click is released.

Before into code:

  • OnMouseDown() - OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
  • OnMouseUp() - OnMouseUp is called when the user has released the mouse button.

Based on these definitions, I assumed (mistake?) OnMouseUp() would be called by the object that above it, the mouse left click was released.

On the Tile script, I have the following :

public class Tile : MonoBehaviour
    // variables
    private Vector2 _gridPosition = Vector2.zero;

    public Vector2 GridPosition
        get { return _gridPosition; }
        set { _gridPosition = value; }

    // functions
    string Vec2toText(Vector2 v)
        return String.Format("(" + v.x + ", " + v.y + ")");

    void OnMouseDown()
        Debug.Log("TILE:: ON_MOUSE_DOWN:: " + Vec2toText(_gridPosition));

    void OnMouseUp()
        Debug.Log("TILE:: ON_MOUSE_UP:: " + Vec2toText(_gridPosition));

    void OnMouseEnter()
        Debug.Log("TILE:: MOUSE_ENTER: " + Vec2toText(_gridPosition));

    void OnMouseExit()
        Debug.Log("TILE:: MOUSE_EXIT: " + Vec2toText(_gridPosition));

And here is the output:

TILE:: MOUSE_ENTER: (15, 15)
TILE:: MOUSE_DOWN: (15, 15)
TILE:: MOUSE_EXIT: (15, 15)
TILE:: MOUSE_ENTER: (14, 15)
TILE:: MOUSE_UP: (15, 15)    %% This line
TILE:: MOUSE_EXIT: (14, 15)

I would expect OnMouseDown() to be called by the object on which the mouse button was pressed (no problem here) and OnMouseUp() to be called by the object on which the mouse button was released (instead, it is called by the same object as OnMouseDown()).

Is this -- calling two functions by the same object even though I pressed left click some object and released it on some other object -- intended?


1 Answer 1


I believe it is, because otherwise an object which is clicked won't be able to determine when the mouse button is released again.

The best alternative I can think of is raycasting.

Put all objects which can possibly be in front of the tiles in the Ignore Raycast layer.
Then, from a "manager" script, cast a Raycast from the mouse cursor's position into the world, check which tile is being clicked, store the clicked GameObject in a variable lastClicked, and call some kind of method in its Tile script to tell that it was clicked, each update.
When the current hovered object is no longer equal to the lastClicked object, call some kind of method in the Tile script to tell that tje tile is no longer clicked.

Just ask me if you want a code example!

Best regards

  • \$\begingroup\$ Thanks for the answer! Code example won't be necessary, I managed it with a static state variable and OnMouseXXX() calls. \$\endgroup\$
    – Varaquilex
    Commented Apr 1, 2015 at 21:45
  • \$\begingroup\$ @Varaquilex No problems! By the way, you might want to consider asking questions like this on Unity's own answers hub, which may give you more detailed answers. \$\endgroup\$ Commented Apr 1, 2015 at 21:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .