• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by hector78 · Nov 24, 2009 at 10:47 PM · animation

How to select an animation clip by index number?

I have an created an animation in the editor with multiple clips,

How can I play a specific clip (for example the second one)?

I know the scripting reference explains how to select it by clip name, but is there a way to play it by index number?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

7 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Kerihobo · Jul 27, 2015 at 10:08 PM

I know this question is old, but I thought I'd share my fix.

I had 80 animated characters all with the same 5 animations, though some of them were named differently because it would be the walk cycle for a crawling baby or an old man with a walking stick, therefore different from the walk cycle of my generic humans.

What I did was create a script that just had a variable for animationClips.

 var animList : AnimationClip[];

That's it, that's all my script had lol. Then I applied it to the prefabs of all my 80 chars, and listed their animations in respective order:

bind walk flinch dance suicide

Then in script I just said

 gameObject.animation.Play(getComponent(AnimList).animList[0]);

basically I couldn't access THEIR array so I made my own array that I COULD access.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image petersvp · Sep 05, 2016 at 12:03 AM 0
Share

And you cannot then edit the animations easily into the Animation Window. This is, it just does not like custom component with AnimationClip references.

avatar image
1

Answer by Jaap Kreijkamp · Nov 24, 2009 at 11:19 PM

I believe you can't, stupid, isn't it? You CAN however iterate over the animationclips with the (in C#) foreach keyword:

foreach (AnimationClip clip in animation) {
    // do initialisation or something on clip
}
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BerggreenDK · Oct 20, 2011 at 09:01 AM 0
Share

Sorry, but this doesnt work?

InvalidCastException: Cannot cast from source type to destination type.

avatar image gfr · Oct 20, 2011 at 09:23 AM 1
Share

It's AnimationState, not AnimationClip - BerggreenD$$anonymous$$, my answer on your question will be visible when approved by a moderator.

avatar image BerggreenDK · Oct 20, 2011 at 10:15 AM 0
Share

you need to use AnimationState not AnimationClip

avatar image BerggreenDK · Oct 20, 2011 at 10:19 AM 0
Share

Thanks, didnt see it before posting my own. I've upvoted your comment.

avatar image twobob · Jan 08, 2015 at 09:16 PM 0
Share

Doesn't this implicitly mean that all legacy operations requiring this type of iteration are doomed to create garbage?

avatar image
1

Answer by shopguy · Feb 23, 2015 at 05:10 AM

Kind of simple, but if it saves someone some time...

 string ClipIndexToName(int index)
 {
     AnimationClip clip = GetClipByIndex(index);
     if (clip == null)
         return null;
     return clip.name;
 }

 AnimationClip GetClipByIndex(int index)
 {
     int i = 0;
     foreach (AnimationState animationState in animation)
     {
         if (i == index)
             return animationState.clip;
         i++;
     }
     return null;
 }

Update, since the above seems to have broke with Unity 5 (or maybe was always fragile and now I'm just finding out), I ended up changing GetClipByIndex to this. Too bad I have to hard-code the clip names, a little extra work:

 AnimationClip GetClipByIndex(int index)
 {
     string[] ClipNames = { "StepForward", "StepBackward", "ShortThrow", "LongThrow", "BackwardThrow", "SoftThrow", "Bull", "Hurt" };
     Animation animation = GetComponent<Animation>();
     return animation[ClipNames[index]].clip;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shopguy · Mar 15, 2015 at 11:17 PM 0
Share

FYI - This seems to have broken as of Unity 5... or at least, the clips aren't always in the same order, the order you would expect (what they are in the editor), which kind of defeats the purpose of using an index to begin with, if you can't be sure of the order.

avatar image
0

Answer by Charles-Van-Norman · Jan 18, 2011 at 05:43 AM

Animations in Array: Someone has tried to make a unique class to attach to the gameobject you're animating. I can't get it to work, but I understand the idea ..... I think. Anyway here is the thread: http://forum.unity3d.com/threads/64262-Animation-clips-in-array-!!?highlight=animation+clip+index

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by BerggreenDK · Oct 20, 2011 at 10:18 AM

Here is the way to index AnimationClips or AnimationStates as they are called:

     GameObject goPlayer = ... some object with animation
 
     int animCount = goPlayer.animation.GetClipCount(); // clip?! Unity people, why not state??
     Debug.Log("Animations found: " + animCount );
     
     int idx=0;
     foreach (AnimationState anim in goPlayer.animation) 
     {
         // show me my animation name!!!
         Debug.Log("Animation ("+ idx +"): " + anim.name);
         idx ++;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I make animations snap to a frame? 2 Answers

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers

Animation set start time 0 Answers

How can the animation on one bone drive another bone? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges