Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Lisk · Apr 30, 2015 at 03:51 PM · animator

Check Animation State exists before playing it

Suppose I have an Animator anim. I call:

 anim.Play("attack")

which works fine, if the "attack" animation exists.

But for cases when the animation state does not exist, I get an error at runtime.

 Animator.GotoState: State could not be found

Question: How can I check that "attack" exists before playing it?

I thought I'd use Animator.HasState() but I don't know what to pass it.

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
5
Best Answer

Answer by bartm4n · Apr 30, 2015 at 04:13 PM

EDIT: I did a little more digging, and Animator.HasState() is actually pretty easy to use.

http://unity3d.com/learn/tutorials/projects/stealth/hashids http://docs.unity3d.com/ScriptReference/Animator.StringToHash.html

EDIT2: The rest of this really only works if you are using the legacy animation system, sorry..

If that doesn't work for some reason, you could also enumerate all of the animation states and check for the one you are looking for by modifying the code sample from the manual.

http://docs.unity3d.com/ScriptReference/Animation.html

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     public Animation anim;
     void Start() {
         anim = GetComponent<Animation>();
         foreach (AnimationState state in anim) {
             if(state.name == "attack") {
                 //Do your stuff here
             }
         }
     }
 }

I'm not sure how much of a performance hit this generates, but if it is something you are doing a lot it might be better to establish some logic in your script that helps it determine if it can attack or not without needing to enumerate anything.

Comment
Add comment · Show 3 · 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 Lisk · Apr 30, 2015 at 04:18 PM 0
Share

Hmm, I'm using "Animator" rather than "Animation", so I can't seem to enumerate them as you've done.

avatar image bartm4n · Apr 30, 2015 at 04:23 PM 0
Share

Oops, you are right. Legacy vs $$anonymous$$ecanim =/ I think that my edit should help you though..

avatar image cj-currie · Nov 11, 2021 at 12:41 AM 0
Share

I searched for this for a while. Thank you!

avatar image
0

Answer by VCC_Geek · Oct 16, 2018 at 05:19 PM

If you're worried about a performance hit, the one thing I'd recommend is using a for loop rather than foreach. There's a performance hit any time you use foreach, because it has to create an iterator, which runs in linear time, whereas a for loop doesn't need to create anything. So really, it's only an issue of how many objects you're searching. Three to five: that happens in a Planck era on any modern processor - use whatever the heck you want. Three to five thousand (or arbitrary): you might want to optimize a bit.

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 JannickL · May 02 at 05:27 PM

For animator you can use:

 var stateId = Animator.StringToHash("Movement");
 var hasState = anim.HasState(0,stateId);

You can use this code. Just change "Movement" to what ever state you are looking for and "0" to your animator layer index :-) Cheers

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

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

23 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 avatar image

Related Questions

2D Animation does not start 1 Answer

2D animator vs. Sprite Array? 1 Answer

Rough change of animation states 0 Answers

Apply Root Motion with Animator and Animation Made INSIDE Unity 0 Answers

Children won't animate 2 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