Attaching animator to Script

Hello, I’m making a 2D melee fighting game. I managed to get my character move from left to right but I am not able to make my animations play while he’s moving. I’m getting the error

" MissingComponentException: There is no ‘Animation’ attached to the “Player” game object, but a script is trying to access it. "

Now, in my player I have Sprite Renderer with a static image, Animator linked to the Animator with the animations, Player Controller script and a 2D polygon Collider.

I’m using the following code.

#pragma strict

var speed: float = 20;
private var anim : Animator;

function Start() {
anim = GetComponent(Animator);

function Update () {
		transform.Translate(Vector3(speed,0,0) * 50 * Time.deltaTime);
	transform.Translate(Vector3(-speed,0,0) * 50 * Time.deltaTime);

    	Debug.Log("Fire1 button pressed");
   if (Input.GetAxis("Horizontal") > 0.1)
       animation.CrossFade ("walk");
      animation.CrossFade ("idle");

You don’t need an Animator, you need an Animation component (which holds the animation clips). Note that you actually call the animation component of your object, so it’s no wonder you get an error message saying that there is none! :slight_smile:

The animation component is the one you’re looking for. If you are planning on using the script for multiple characters, you could add it via the script if the component is missing on the gameobject.