# Can't assign animation to animation variable declared in script Unity2D

So I'm trying to have my script pause an animation when the user clicks. Here's what I have so far:

I defined the animation up at the top

public Animation anim;

and then further down,

	void Update () {
if (Input.GetMouseButtonDown(0))
{
anim["myAnimation"].speed = 0.0f;
}

}

Code gives no error, however in Unity I have a GameController object to which this script is attached to. The gamecontroller object has a vacant field into which I drop my animation to. The issue is that when I try and drag and drop my animation or try to place it into that field, nothing happens, I keep getting the error down at the bottom saying that "The variable anim of GameController has not been assigned." How do I go about assigning myAnimation to the variable anim?

When I try and drag and drop my animation file to this, nothing happens. This is under the GameController script which I assigned to a GameController object.

• Are you sure everything is well set? Remember that some properties are only available in the AnimationController. – DH. Sep 19 '16 at 19:14
• make sure animation is set to "Legacy" go to project tab find your anim, press it then in top right corner find small menu(three small lines) and press and go to debug mode. here yo change it to legacy. – Nick Oct 30 '16 at 8:53

First of all, you must clarify which animation you are talking about.

This one

Or this one

The first one is a Component, which can be assigned to public field of script in the Inspector, the second one is a animation clip, which can be attached to animation controller like Animation or Animator.

So if you are trying to drag-drop an animation clip into an Animation field, nothing will happed.

• It's the second type (AnimationClip), so how do I achieve the same thing of being able to pause my animationClip in my script? – FShiwani Sep 24 '16 at 0:14
• Add Animation component to the GameObject, drag-drop it in inspector into the Animation public field that you declared in your script. Next drag-drop the Animation clips into the "Animations" list as seen above in first image in Animation component. Im on phone so cant test here but I think it should work. This is the legacy animation system (the old one) so i didnt play with it much, the newer one is called Mecanim-I usually work with it. – tc07 Sep 24 '16 at 0:24
• Basically. I have an object called GameController which is currently handling spawning of some enemies(through an attached GameController script.). I have another object which on collision with these enemies needs to stop. My plan was to handle all of this in the same GameController script. This other object has its own animator which is running an idle animation clip. Is it possible to drag an animation component from this other game object to the inspector Animation puiblic field on my GameController object? – FShiwani Sep 24 '16 at 0:50
• Alright. I've been mixing up Animator and Animation. That's my mistake. Is there any way to get the gamecontroller script to disable an animator attached to a different object. I don't want to attach my game controller script to the other object – FShiwani Sep 24 '16 at 0:59
• I think it's completly possible to archive that. However, I can't give the the solution here, what you are asking is already out of the scope of this question. I propose you to make new question, give details as much as possible (better with illustrations), there will be the right place to discuss and I will be able to give my best to help. – tc07 Sep 24 '16 at 1:12

You have just run into the difference between declaration and definition of a variable. Just because you did this:

public Animation anim;


Does not mean that you're done! This variable currently holds the value null which means "nothing is here." Attempting to act upon a null object at runtime will cause the application to crash with a NullPointerException ("you tried to access something that doesn't exist").

In order to turn a declaration ("this is a variable") into a defined object ("this is a variable with a value") you need to assign something to it, either by using the assignment operator (=) or by using Unity's property inspector. What you assign this variable to, however, is entirely on you to figure out (you haven't supplied enough information about your project).

• I'm trying to assign an animation to my anim variable through the Unity UI(Property inspector as you said). That's where my issue is, when I declare GameObject variables in my script I can just drag and drop my game object from unity to the empty field in the inspector, however with animations when I try to drag and drop it nothing happens. I've added a screenshot in the original post – FShiwani Sep 19 '16 at 20:04
• I'm trying to have an animation pause for a certain object when the user left clicks. I have created an animation controller for that object and created an idle animation state called myAnimation for test reasons. That state is linked with the myAnimation animation file which I made from several sprite frames. I thought I could have the animation pause by setting it's speed to 0. docs.unity3d.com/ScriptReference/AnimationState-speed.html – FShiwani Sep 19 '16 at 20:15
• Animation is a component You're probably looking for AnimationClip – Draco18s no longer trusts SE Sep 19 '16 at 20:46
• If I replace Animation with AnimationClip in my declaration, I get this error: Error: Cannot apply indexing with [] to an expression of type 'AnimationClip' – FShiwani Sep 19 '16 at 22:00
• Ok, so, the speed attribute applies to Animation. If you want to do that you need to get the animation component – Draco18s no longer trusts SE Sep 19 '16 at 22:42