Calling animator from script.

Hello, I’m making a 2D Hack and slash. I managed to make my character move left and right but now I want it to play the walk animation while doing so. I already have the animator attached to my player but when I press play this comes up on the console.

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

Below is the code I’m using.
#pragma strict

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

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

function Update () {
	if(Input.GetKey("d")){
		transform.Translate(Vector3(speed,0,0) * 50 * Time.deltaTime);
		animation.Play("walk");
	}
	
	if(Input.GetKey("a")){
	transform.Translate(Vector3(-speed,0,0) * 50 * Time.deltaTime);
	animation.Play("walk");
	}

	if(Input.GetButton("Fire1")){
    	Debug.Log("Fire1 button pressed");
    	animation.Play("swordSlash");
    }
    
   if (Input.GetAxis("Horizontal") > 0.1)
       animation.CrossFade ("walk");
   else
      animation.CrossFade ("idle");
 
}

Thanks!

FierceOBeard

Change all your “animation.Play” calls to “anim.Play” instead. That is, after all, why you’re setting the anim variable in your Start function.

Oh! Now I feel silly xD Thanks!