Reacting to Mouse animation...

I’m trying to make animations that react to the player’s mouse. I looked all over the unity reference site and nothing works. What am I missing?

function Update () {
}

function OnMouseEnter () {
	animation.Play("Element 1");
	yield WaitForSeconds (1);
	animation.Play("Element 0");
}

function OnMouseDown () {
	animation.Play("Element 2", PlayMode.StopAll);
}

First thing to check is wether the functions are called at all, e.g. using Debug.Log().

If they are not:

  • Check that you have a collider on your object, otherwise OnMouseEnter()/OnMouseDown()/… will not be called:

OnMouseEnter is called when the mouse entered the GUIElement or Collider.

  • Beware that they are not called for objects on the “Ignore Raycast” layer.

All 3D interaction between gameobjects and the mouse needs be done through COLLIDERS. Colliders are responsible of intercepting events between gameobjects and the world, basically.

1 add a collider of any type to your gameobject

2 add the following code in a c# script that you attach to the gameobject

void OnMouseDown() {
	Debug.Log("AHAHA stop clicking me!");
}

3 in play mode click over the gameobject