• 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
1

Answer by splitter20 · Mar 28, 2017 at 03:02 AM

Not sure if this is how it's supposed to work, but this does exactly what your asking.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyMovement : MonoBehaviour {
 
     Animation anim;
     public List<string> animNames;
 
     void Start(){
         
         anim = GetComponent<Animation> ();
 
         foreach (AnimationState clip in anim) {
             animNames.Add(clip.name.ToString());
         }
     }
 
     void Update(){
 
         if (//something happens//) {
             anim.CrossFade (animNames [1]);             //plays animation in element1 spot
         } else {
             anim.CrossFade (animNames [0]);             //plays animation in element0 spot
         }
     }
 }
 




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 Antonius007 · Mar 21 at 08:34 PM 0
Share

Thanks this works, but I think you forgot to initialize empty list like this: private List<string> animNames = new List<string>();

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
  • 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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Can I make animations snap to a frame? 1 Answer

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers

2D game animation is running 1 Answer

need animation help 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges