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
0
Question by HalversonS · Apr 08, 2014 at 07:09 PM · animationjavascriptanimation clipanimation scriptanimation.play

Play either the first or second animation on an object without specifying its name

I have multiple gameobjects throughout the scene that have two animations. I need to play either the first or second animation without changing their imported animation names.

Is there a way I can include in the function for it to call the animation using its element? Like element 0, element 1, as found in the inspector.

Bare with me, I do modeling/texturing/animation. Some scripting makes sense then I run into something I can't figure out like this. Thanks for any help!

Currently I have:

 #pragma strict
 
     function OnMouseOver()
     {
         if(Input.GetMouseButtonDown(0))
         {
        animation.Play("r_gear_down");
         }
     }

This works just fine, however I would have to write over 500 individual scripts for each separate game object. I'd like to write one script and add it to each one instead. I monkeyed around with trying to identify animation clips with no success.

Thanks! (I used the format code button, but it appears it didn't want to give the lines. Sorry!)

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

1 Reply

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

Answer by thomasindustry · Apr 08, 2014 at 10:16 PM

My javascrtipt is super rusty, if you get a list of all the names of the AnimationStates, you should be able to play them by element.

 var animNames = new Array();
 var currAnim: int = 0;
 var theAnimation: Animation;
 
 function Start () {
 
     theAnimation = gameObject.GetComponent(Animation);
 
     var animationCount: int = 0;
     for(state in theAnimation)
     {
         var nextState : AnimationState;
         nextState = state;    
         animNames[animationCount] = nextState.name;    
         animationCount++;        
     }
 
 }
 
 function OnMouseOver()
 {
     if(Input.GetMouseButtonDown(0))
     {
         currAnim = (currAnim + 1) % animNames.length;    
         theAnimation.Play(animNames[currAnim], PlayMode.StopAll);
     }
 }
Comment
Add comment · Show 2 · 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 HalversonS · Apr 09, 2014 at 12:08 PM 1
Share

Perfect! Exactly what I needed. I was able to add a bit to get it to play between elements 1-2 and 3-4 depending on what state some other gameobject animations are in. Thanks!! (Alas I am new so I am unable to give it a thumbs up, something about 15 rep... but thumbs up!)

avatar image thomasindustry · Apr 09, 2014 at 02:58 PM 0
Share

Glad to hear it :)

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

22 People are following this question.

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

Related Questions

How would I go about carrying out another object's animation IN C#! 1 Answer

Animations Not Working 2 Answers

How to loop unity?JS 3 Answers

Animation doesn't work 1 Answer

Play simple animations on demand 1 Answer


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