# Animation works, but Unity doesn't realize it

I have an animation which plays once, and it played once just fine. I want to Invoke a method as soon as the animation finishes. I have been researching and experimenting for over an hour now, but without the result I want. I try to get the animation from the Animator, but Unity says my animation is not attached. The thing is, I don't care if it's attached...I just want to know how long it is, not actually do anything with it. How can I achieve this? Here is the code which results in the error:

CODE:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(PolygonCollider2D))]
[RequireComponent(typeof(Animator))]
public class Construction : MonoBehaviour
{
// Use this for initialization
void Start()
{
Invoke("EnableObstacleState", GetComponent<Animator>().animation.clip.length);
}

private void EnableObstacleState()
{
GetComponent<PolygonCollider2D>().enabled = true;
}
}


ERROR:

MissingComponentException: There is no 'Animation' attached to the "Barricade(Clone)" game object, but a script is trying to access it.
You probably need to add a Animation to the game object "Barricade(Clone)". Or your script needs to check if the component is attached before using it.
Construction.Start () (at Assets/Scripts/Construction.cs:11)

• Getting information about how long the animation is, is doing something with it. You should care if the animation is attached, because you need to access it to find out how long it is. – MichaelHouse Jan 24 '15 at 17:34

Let's unpack this, as it looks like you might be confusing Animator & Animation

GetComponent<Animator>().animation.clip.length


GetComponent<Animator>() returns an Animator component attached to the same game object as this script instance, if one exists.

.animation tries to access a member called "animation" of that Animator object. Animator itself doesn't define such a member. So instead, .animation tries to access an Animation component attached to the same game object as the Animator.

(This makes the initial GetComponent<Animator>() call redundant, which is why I said it looks like there's some confusion here)

If there is no Animation component attached (and the [RequireComponent] attributes at the top don't guarantee there will be), then you can't get any information about it. It's kind of like asking "how long is the longest river that never existed?" - there's no meaningful way the engine can answer this for you.

What you might be looking for instead is:

GetComponent<Animator>().GetCurrentAnimatorClipInfo(layerIndex)

to get information about the current animation clip playing on a particular layer

• woah i forgot all about this question. i dont even have the same project that created this issue. i cannot really confirm or deny whether or not this is the correct answer. so ill meet you in the middle, and give you a checkmark but no upvote – Evorlor Apr 24 '15 at 18:46

Check the barricade prefab to make sure the animation is set to the animation you are trying to invoke.