Accessing Animations with an Index

Im shocked no one has asked this or run into problems with it so far. Unless I’m just overlooking something that was addressed - but I couldn’t find it, so how should I expect anyone else would?

So, in case you’re like me, you had a problem with this at some point and maybe you were smart enough to figure out or you found a work-around, but for quite a while I banged my head against the wall. Then half way through writing this post, I figured it out and decided to instead post my solution.

As it stands, you would access the animations of a game object by doing something like the following:

SomeGameObject.animation.Play("Walk");
SomeGameObject.animation["Walk"]

What was interesting to me, was that you couldn’t access these with an index.

SomeGameObject.animation.Play(0);
SomeGameObject.animation[0];

Even more surprising, is that there is the animation.GetClipCount() function to get the number of clips in your animation, which made me think there was support somewhere for getting to animations with an index otherwise what else would you use this function for but setting up a loop to iterate through your animations?

Solution: While I would still like the be able to access animations in the animation of a GameObject with an index(Would feel right), the foreach has really made itself useful to me, which is rare, and also why I probably didn’t think to try this sooner:

//C#
foreach(AnimationState animstate in someGameObject.animation)
	Debug.Log(animstate.name);

… I know, DUH! Right??? While I’m still not accessing with an index, I could easily use a loop like this to create my own array of the animation states for later use with an index.

Anyways, I hope this helps someone later.

I ran into this just today! I always just need the zeroth index of two animations and instead of indexing with just 0 I have to pass in two different names to index from. Ugly AND slower.