Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 co$$anonymous$$g, 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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Trim animation to remove middle 1 Answer

Animation marked as legacy, still wont work 1 Answer

Animation won't play EVERY time it is called 1 Answer

Animation precision? 0 Answers

How to handle animations properly? 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