Unity – OnMouse

OnMouse is called when the user has used the mouse over the GUIElement or Collider.

Basic

1. Create an object
2. Assign a Collider OR ONMOUSE WILL NOT WORK!
3. Attach this script and click over the object with the mouse button:

#pragma strict

function Start ()
{
   
}


function OnMouseDown ()
{
    Debug.Log('Activaction of OnMouseDown!');
}

NOTICE: function OnMouseDown is outside other functions, you do not need put it inside Update() function.

Complete list of OnMouse functions

#pragma strict

function Start ()
{
   
}
// On Mouse Click events ###############################################

function OnMouseDown ()
{
        // When you click over the object
        Debug.Log('Activaction of OnMouseDown!');
}

function OnMouseDrag ()
{
        // When you click over the object and drag
        Debug.Log('Activaction of OnMouseDrag!');
}

function OnMouseUp () {
        // When you release the drag over or outside the object
	Debug.Log('Activaction of OnMouseUp!');
}

function OnMouseUpAsButton () {
        // When you release over the object
	Debug.Log('OnMouseUpAsButton');
}

// On Mouse position cursor events ######################################

function OnMouseEnter () {
	// When cursor enter in the object area
	Debug.Log('Activaction of OnMouseEnter!');
}

function OnMouseOver () {
	// When cursor is over the object area
	Debug.Log('Activaction of OnMouseOver!');
}

function OnMouseExit () {
     // When cursor leave object area
	Debug.Log('Activaction of OnMouseExit!');
}