I am trying to get my first animations working in Unity and I’m not sure what I’m doing wrong here. I have created a simple model with two simple animations (Actions) in Blender, one for idle one for jumping. It’s a simple sphere that bounces.
These are my steps:
- Drag .blend file into Unity
- Set Animation Type to Legacy under rig tab.
- Configure the settings for each animation in the Animations tab. Apply all my settings.
- Drag my new game object into my scene
- Select the new object in my scene
- Add the component
Animation
. Set default to my idle animation, include both of my animations in the list of elements. Element0=bounceNormal, Element1=standingStill
I created a script called BallBehavior, these are its contents:
using UnityEngine;
using System.Collections;
public class BallBehavior : MonoBehaviour {
void Start () {
}
void Update () {
if(Input.GetKeyDown("f"))
animation.Play ("bounceNormal");
}
}
I drag this script to the object I put into my scene. Run the game. The ball jumps as expected when I press f… but I get the error:
MissingComponentException: There is no 'Animation' attached to the "Ball" game object, but a script is trying to access it.
You probably need to add a Animation to the game object "Ball". Or your script needs to check if the component is attached before using it.
UnityEngine.Animation.Play (System.String animation) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/Animations.cs:568)
BallBehavior.Update () (at Assets/GameObjects/BallBehavior.cs:14)
I am actually able to get rid of this error by adding an Animation
component to the ball’s mesh within the object hierarchy. But that means I have to add the same animation components twice for my object, which doesn’t seem right. My animation works, but playing it from the script gives me problems.
What am I doing wrong here? Any help appreciated.