How to get a reference of "Animations" array?

Hello,

I'm trying to figure out how to get a reference to the "Animations" array that the Animation component holds onto.

You can set this up in the Editor to hold more than just your default clip, and you can gain access to these clips in a variety of ways. You can animation.GetClipCount to find out how many are in there, you can play an animation directly by inputting a string of the animation's name: animation.Play("NewAnimation"), but I cannot figure out how to access the animation clip simply by it's position in the array.

I can't seem to find any reference as to how to get to the "Animations" array nor how to play a clip by any method besides using a string or adding the clip to the array.

Thanks!

Generally you iterate through the animation enumerable to get at the AnimationState objects:

foreach(AnimationState state in animation)
{
    //do something with state
}

You could also add them all into a list fairly easily, using linq to cast the enumerable (or use an arraylist instead i guess)

List<AnimationState> states = new List<AnimationState>(animation.Cast<AnimationState>());

or

ArrayList states = new ArrayList(animation);

From the state, you can get at the name for playing, or even grab the clip it refers to (.name and .clip respectively)

This code should work with C# to print all the AnimationClip names.

Just pass in a gameObject with an Animation Component.

public void PrintAnimations(GameObject character)
	{
		Animation anim = character.GetComponent<Animation>();
		foreach(AnimationState state in anim)
		{
			Debug.Log(state.name);
		}
	}

Also you could play the animation by calling …

anim.Play(state.name);

Seems like there should be. Using MonoDevelop and the auto-field (whatever that's called) looks like there's some undocumented things. Animation.GetClip("clipname") for instance. You might poke around there. How to get a list of clip names, I don't know. You might need to use Reflection