- Home /
Access Animations array in Animation component
In the animation component, there is the default Animation, and also an Animations array. How do you access a particular animation in this array, for play at an arbitrary time during run time?
The obvious animation.animations[index]
does not work.
Answer by testure · Jun 30, 2011 at 03:32 AM
the array you're accessing in your example is the animation state- so it's still useful for your purposes, just not in the way you were using it.
try something like:
animation.CrossFade(animation.animations[index].name);
same error: BCE0019: 'animations' is not a member of 'UnityEngine.Animation'. Did you mean 'animateOnlyIfVisible'?
er.. sorry. that's what I get for hastily doing a copy/pasta.
you actually cant access animations by index. not out-of-the-box anyway. the easiest way to do something like this is to build your own array/list and add them that way.
example:
List<string> animations = new List<string>();
void Start()
{
foreach(AnimationState state in animation)
{
animations.Add(state.name);
}
}
void Update()
{
// now you can do whatever you need to do by index.
}