I am trying to start an animation by pressing a key on my keyboard.

For this I am using the following code:

[HideInInspector] public Animation animation;

private void Start()
    animation = GetComponent<Animation>();

private void Update()
    if (Input.GetKeyDown("1"))

This gives me the following error:

The animation state AnimationName could not be played because it couldn't be found!

Although, the animation DOES start, but how can I clear this error?

1 Answers

Mah On

I think the only possible explaination is that your animation clip is not marked as Legacy. https://www.unity3dtips.com/zh/the-animation-state-could-not-be-played-because-it-couldnt-be-found/

EDIT with step by step here:

Animations must be marked as legacy to be used with the animation component.

  1. In the project window select the animation clips you’re trying to play.

  2. Set the inspector to debug mode which exposes hidden variables, in this case it’ll make the “Legacy” checkbox appear.Unity Inspector Debug Mode

  3. Tick the “Legacy” checkbox and change the inspector back to normal mode. Unity Inspector Animation Clip Legacy Checkbox

But there is one thing you should know: you should avoid, if possible, the old legacy way to play animation. Learn how to use an Animator component. Just create an animator, assign your animation clip, decide a boolean for start the animation in the animator component and set it to true in your code (for basic use). Sure there is some things to do beside code but it will be easier to control your animation, bleand different animations and change them to your needs (it's for example much easier to loop or stop the animation at some point). It would be also easier to understand when it start and when is playing.