Why doesn't this animation script work?

function Start () {
animation.Play();
}

function Update () {
	if (Input.GetAxis ("Horizontal") < 0) {
		animation.Play("walkL");
		}
	else if (Input.GetAxis ("Horizontal") > 0) {
		animation.Play("walkR");
		}
	if (Input.GetAxis ("Vertical") < 0) {
		animation.Play("walkB");
		}
	else if (Input.GetAxis ("Vertical") > 0) {
		animation.Play("walkF");
		}
}

This is an animation code for my character. I think it’s obvious what I’m trying to do, (Animate WASD movement) but I keep getting errors and my character doesn’t animate.

The animation state walkF could not be played because it couldn’t be found! Please attach an animation clip with the name ‘walkF’ or call this function only for existing animations.
UnityEngine.Animation:Play(String) scriptRockanoidAnimate:Update()
(at Assets/scriptRockanoidAnimate.js:16)

Also this error:

Default clip could not be found in attached animations list.
UnityEngine.Animation:Play()
scriptRockanoidAnimate:Start()
(at Assets/scriptRockanoidAnimate.js:2)

//then simply call the function like this
if(Input.GetKeyDown(KeyCode.XX) {
PlayAnimation(“animation name as string”);
}

//copy below code to your script that object attached animations
//function about play animations
function PlayAnimation(AnimName : String) {
	if (!animation.IsPlaying(AnimName))
	animation.CrossFadeQueued(AnimName, 0.3, QueueMode.PlayNow);
}


i hope this is helpful to you :D

For anyone else. I have fixed the problem. Animations need to be set as Legacy.
In the rig tab of the object. Change where it says Generic to Legacy. Now it works! :smiley:

Thanks. This works :slight_smile: