On Mouse Over

I have one problem and I can solve it. Can you help me? I have two objects, a cube and an avatar. Avatar tag is key. I want when the cursor touches the cube to play the animations in avatar (key). And when it leaves the cursor to stop playing the animations. I make the animation in unity with name right. The problem which write in console is
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) .

I have the follow script in my object cube.

 function OnMouseOver() 
   var Cube : GameObject[];
 Cube = GameObject.FindGameObjectsWithTag("key").animation.Play("right"); 

The null reference exception likely comes from this line.

Cube = GameObject.FindGameObjectsWithTag("key").animation.Play("right"); 

This is saying that the Cube “equals” the whole line, not just the FindGameObjectWithTag portion. As such, you are setting the Cube equals to the return of


To fix this, you can split the code into two lines:

Cube = GameObject.FindGameObjectsWithTag("key");

or, if you aren’t using the cube variable you can just say:


and just get rid of

var Cube : GameObject[];

You have a null pointer in that line, check what’s null.

It could be:

  • GameObject.FindGameObjectsWithTag(“key”) ← You made a mistake with the tag on your avatar or it is disabled.
  • GameObject.FindGameObjectsWithTag(“key”).animation ← Your avatar does not have an “Animation” component, maybe you have an Animator. In that case I think “.animator” works, but if it doesn’t you can do “.GetComponent()” instead.

If you are making the animation in unity itself then you have to check the inspector if it contain component
of animation rather than animator .
then only you can play the animation directly with animation.Play

if you are having problem getting the animation component just let me know

or I will suggest to use mechanm with the help of animator

Guys are all right. The problem was that although I tag my model for some reason it was not working. I do not know what really went wrong. Thank you! I used the following code.

  function OnMouseOver() 
    var theanimation=GameObject.Find("2015 key").animation.Play("right");