I make an animation for an object in my scene and attach it as a component to the object I am animating. The problem is that when I go to trigger my animation (as shown in the script below), it gives me the error “The animation state Attack could not be played because it couldn’t be found!” I have searched the web for solutions and some people say that I should change the animation to Legacy but I don’t know how to do that because I am not importing it. Any help would be greatly appreciated. The video I am learning from is: 3. Unity Tutorial Animation and Health - Create a Survival Game - YouTube
Take a quick gander at the inspector; you see how the Animation
component has an entry named “Animation” and another named “Animations”? The first is the “default clip” that’s played if you don’t specify a name; the other is a list of clips that can be referred to by name.
This is confusing, and it’s generally best to use one or the other. The key difference is whether you have one animation to control, or several animations to choose from.
If you look at the script reference page for Animation.Play, you’ll notice there are two signatures for the function:
Weapon.animation.Play(); //play default clip
Weapon.animation.Play("Attack"); //play clip named "Attack"
I can’t verify this with the information you’ve given me, but I’m betting that you don’t have “Attack” listed under “Animations”? You can either set it there, or just play the default clip directly.
(Small disclaimer: it’s been a while since I’ve mucked around with legacy animation code in detail.)
That is because you must add you animations to that “ANIMATIONS” , that is a array and you can add your animations there by drag and drop and then go to script and do
Animation anim;
anim.Play("xAnim");
If you have just one animation then you drag and drop one animation , like you have done with “Attack” anim , and you want to play that then you write
anim.Play(); //it plays the default one
anim.Play("xAnim"); //it plays from the array in the inspector