Using foreach on AnimationClips

Hello All!

I’m doing something wrong, and I could use a outside pair of eyes.

I have a array defined of Animations:

public Animation myAnimations;

I have 10 items in this array. Each of the items is a GameObject that has Animations, and inside those Animations are AnimationClips.

What I am trying to do is loop through my array (myAnimations) and then foreach item in the Array, look at all the AnimationClips tied to the item.

It’s something like this:

Code:

    int index = 0;
    Animation myTest;
    foreach (Animation i in myAnimations)
    {
        myTest = myAnimations[index].animation;

        Debug.Log("Test: " + myTest.GetClipCount());

        //foreach (AnimationClip j in myTest)
        //{
        //    Debug.Log("ClipCount: " + j.name);
        //}
        index++;
    }

But I will get “InvalidCastException: Cannot cast from source type to destination type.” if I uncomment out the lines. The GetClipCount is returning the correct number, but I need to loop through the ClipCount to get their names.

Thanks for any help!

When you iterate over an Animation you get AnimationStates, not AnimationClips. Instead you can just do:

foreach( Animation anim in myAnimations )
{
  foreach( AnimationState animState in anim )
  {
    Debug.Log( "Clip name: " + animState.clip.name );
  }
}