play child animations, IEnumerator?

Hi Guys,

I try to play an animation on a child object, but I only manage to play the default animation. I can get the amount of clips, but I can’t manage to play any other clip but the default. How can I play, say, the second animation on a child? I want to create a generic system that does not require me to know all the animation clip names. I feel this might have to do with IEnumerator but cant get my finger on it.
Any help appreciated!

var usedObject:GameObject;

function useObject () {

print("useObject");
var other = usedObject.GetComponentInChildren(Animation);
other.animation.Play();
print(other.animation.GetClipCount);	

}

It’s not possible to access the animationclips by index. The IEnumerator interface only returns the AnimatioState objects for each added Animationclip. Unfortunately the order in the animations array in the inspector isn’t reflected by the IEnumerator interface. I guess because they use some kind of hashtable internally.

The only reliable way to access AnimationClips by index is to hold your own array of AnimationClips in a script.

Unity want you to access the clips by the animation name. You can either rethink your general design, or manage your own AnimationClip array.

Note: To play an AnimationClip on an Animation component, the clip has to be added to the component. This can be done with AddClip(clip,clip.name) at runtime.

For that you have to use recursion to walk through child objects tree of a game object. Than get each animation component attached to that child;

var usedObject:GameObject;

function playAnimationTree( var obj: Transform)
{

var anim: Animation = obj.GetComponent(Animation);
if(anim) anim.Play();

for(var child: Transform in transform)
{
       var chanim: Animation = obj.GetComponent(Animation);
       if(chanim) chanim.Play();
       playAnimationTree(child);
}     

}

function Update()
{

playAnimationTree(usedObject.transform)

}