• 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
3
Question by Thom Denick · May 07, 2011 at 07:28 PM · animationanimations

How to get a reference of "Animations" array?

Hello,

I'm trying to figure out how to get a reference to the "Animations" array that the Animation component holds onto.

You can set this up in the Editor to hold more than just your default clip, and you can gain access to these clips in a variety of ways. You can animation.GetClipCount to find out how many are in there, you can play an animation directly by inputting a string of the animation's name: animation.Play("NewAnimation"), but I cannot figure out how to access the animation clip simply by it's position in the array.

I can't seem to find any reference as to how to get to the "Animations" array nor how to play a clip by any method besides using a string or adding the clip to the array.

Thanks!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by Mike 3 · May 07, 2011 at 08:06 PM

Generally you iterate through the animation enumerable to get at the AnimationState objects:

foreach(AnimationState state in animation)
{
    //do something with state
}

You could also add them all into a list fairly easily, using linq to cast the enumerable (or use an arraylist instead i guess)

List<AnimationState> states = new List<AnimationState>(animation.Cast<AnimationState>());

or

ArrayList states = new ArrayList(animation);

From the state, you can get at the name for playing, or even grab the clip it refers to (.name and .clip respectively)

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 chanfort · Mar 01, 2016 at 10:04 PM 0
Share

5 years old trick and still works... If you use just

 foreach(AnimationState state in animation)
 {
     //do something with state
 }

there are various surprises coming, like "cannot cast from source to destination" or the last state name is somehow the same as the first one. The correct solution seems to be:

 List<AnimationState> states = new List<AnimationState>(animation.Cast<AnimationState>());
 foreach(AnimationState state in states)
 {
     //do something with state
 }

avatar image
6

Answer by MightyJoe1 · Jan 12, 2015 at 10:19 PM

This code should work with C# to print all the AnimationClip names.

Just pass in a gameObject with an Animation Component.

 public void PrintAnimations(GameObject character)
     {
         Animation anim = character.GetComponent<Animation>();
         foreach(AnimationState state in anim)
         {
             Debug.Log(state.name);
         }
     }

Also you could play the animation by calling ...

...

 anim.Play(state.name);
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 umair_hassan · Jul 01, 2016 at 06:55 AM 0
Share

Simple and perfect. Thanks dude !!!

avatar image
0

Answer by DaveA · May 07, 2011 at 07:55 PM

Seems like there should be. Using MonoDevelop and the auto-field (whatever that's called) looks like there's some undocumented things. Animation.GetClip("clipname") for instance. You might poke around there. How to get a list of clip names, I don't know. You might need to use Reflection

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 Thom Denick · May 07, 2011 at 07:58 PM 0
Share

Yeah, I did notice GetClip is not documented. Hoping someone else had figured this out. Since it's publicly exposed in the editor, I was hoping it could be accessed via script somehow.

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

2 People are following this question.

avatar image avatar image

Related Questions

Mirroring animation clip without mirroring root motion node? 0 Answers

Play animation while button is pressed fails when using boolean parameters. 1 Answer

Blender Animations with Different Starting Rotations 0 Answers

I Need The Help Or Advice Of A Unity Animation Expert 0 Answers

Teddy Bear Mecanim NavMesh (my animations don't work) 0 Answers

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