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
1
Question by Cygon4 · Aug 20, 2013 at 11:40 AM · animatorrandommecanimvariablestransitions

Check Available Variables of Mecanim Animator

Is there a way to check which variables I can set on a Mecanim animator?

I have lots of NPCs with sets of animation clips that I want to select from at random. For example, here's a set of Idle animations:

Idle Animations

So I simply added a Variable called Random100 to all my animators that is, once per second, assigned a random number between 0 and 99, allowing my Mecanim state machine to transition (eg. with 30% likelihood it could transition to StandAround or LookBored, with 10% likelihood it could transition to Stretch, Yawn or ScratchHead.

It would be nice it I could make this Random100 a built-in feature of my controllers that, when present, gets fed a random number regularly. However, I couldn't find a convenient HasInteger() or similar method to detect whether there is a variable called Random100 in my animator.

Is there a way to check for this?

idleanimations.png (34.3 kB)
Comment
Add comment · Show 2
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 lolzrofl · Dec 19, 2014 at 10:17 PM 0
Share

The layout of your animation controller is unsettling...

avatar image meat5000 ♦ · Mar 05, 2015 at 04:43 PM 0
Share

The problem is your pentagram is missing candles and an incantation.

Oh wait, $$anonymous$$ecanim.

4 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by CapeGuyBen · Mar 05, 2015 at 04:40 PM

I know this is quite an old thread but it hasn't really been answered so thought I'd share my solution.

I wrote the following extension method for Animator:

 public static partial class AnimatorExtensions {
 
     public static bool HasParameterOfType (this Animator self, string name, AnimatorControllerParameterType type) {
         var parameters = self.parameters;
         foreach (var currParam in parameters) {
             if (currParam.type == type && currParam.name == name) {
                 return true;
             }
         }
         return false;
     }
 
 }

You can now test if a given parameter exists on an animator instance using:

 Animator myAnimator = GetComponent<Animator> ();
 bool hasMyParam = myAnimator.HasParameterOfType ("MyParam", AnimatorControllerParameterType.Int);

it's also easy to write a version which compares 'nameHash' instead of 'name' if that's more useful to you.

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 kriptite · Jan 14, 2017 at 03:48 PM 0
Share

thanks for the extension mate, I rewrote the code to correct the error at the top, (static partial), not (partial static) i also wrote it to accommodate the situation of a null animator using UnityEngine;

 public static partial class AnimatorExtension {
  
      public static bool HasParameterOfType (this Animator self, string name, AnimatorControllerParameterType type) {
          if(null == self){
              return false;
          }
          var parameters = self.parameters;
          foreach (var currParam in parameters) {
              if (currParam.type == type && currParam.name == name) {
                  return true;
              }
          }
          return false;
      }
  }
avatar image CapeGuyBen kriptite · Feb 10, 2017 at 03:14 PM 0
Share

Thanks for the feedback. I fixed the static partial typo in the original post.

Not sure I agree with testing for null self though. It's not usual to handle null 'this' dereferences gracefully. I would expect an exception (specifically a System.NullReferenceException) to be thrown - as the original implementation will do.

Whatever works for you though :-)

avatar image
0

Answer by Xtro · Aug 20, 2013 at 02:11 PM

When you try to read from a variable, you get an error if it doesn't exist. You can catch that error to see if it exists or not.

This is a very basic programming concept. Not related with Unity at all.

Comment
Add comment · Show 4 · 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 Cygon4 · Aug 20, 2013 at 07:14 PM 3
Share

A very basic program$$anonymous$$g concept is also to not rely on exceptions for normal code flow control. Especially if said exceptions, even if caught, produce a warning in the output window.

This question is related to Unity in that I am asking for a way to check the existence of a variable via the $$anonymous$$ecanim API, which I couldn't find in the docs.

Finally, no offense intended, but I would recommend some humble sauce, especially when one's own recommendation betrays a dangerous half-knowledge of program$$anonymous$$g concepts.

avatar image Xtro · Aug 20, 2013 at 07:30 PM 0
Share

Relying on exceptions is inevitable when the existence check is not present. If you can tell me a better idea, I'll be happy to hear.

Errors in output window can be prevented if you consume the exception. I'm not sure about warnings.

I didn't get what you mean by "half-knowledge". Did you think I have half-knowledge because I recommend the use of exceptions when the existence check is not present ??

avatar image Cygon4 · Aug 22, 2013 at 10:15 AM 0
Share

That's why I asked this question, I was hoping someone might have figured out a better way to check for this.

The warning is sadly generated before the exception is thrown, so even if I set some flag like variableNotPresent the first time I catch the exception, one warning will always trigger. I've now avoided it by adding a public bool ProvideRandomNumbers so I can turn it on and off on a per-case basis in the inspector window.

I just tried to mirror the attitude I perceived from you. Your answer sounded like it would be the right and proper way to rely on exceptions and that I'm wasting people's time even asking for a method to check a variable's existence.

avatar image Xtro · Aug 22, 2013 at 01:25 PM 0
Share

$$anonymous$$y attitude was quite and nice since I was trying to help you for free spending my office time. If you don't like the answer or you are already aware of the answer, please just ignore it quietly.

Btw, I really hate the idea of avoiding exception use. They are there for a reason. Just use it people! (When there is no alternative checking method)

avatar image
0

Answer by TonyLi · Aug 22, 2013 at 03:50 PM

I don't think this functionality has been exposed yet, not even in Unity 4.2. At least I don't see it in any of the release notes or documentation.

I know it's far from ideal, but what about adding a set of Boolean parameters guaranteed to exist on all of your animator controllers that indicate what the controller supports -- for example, a hasRandom100 Boolean that you can check.

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 Xtro · Aug 22, 2013 at 03:52 PM 0
Share

He already thought of that idea.

C'mon guys. This is uglier than the exception testing.

avatar image
0

Answer by meat5000 · Mar 06, 2015 at 01:18 AM

You mean parameters?

Use this recent addition to the API (I think)

http://docs.unity3d.com/ScriptReference/Animator-parameters.html

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 Nido · May 27, 2015 at 09:26 AM 0
Share

Strange implementation... Trying it right now. But with this you need to check if a concrete parametrer exists in the array. Won't be better to use a lyst?

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

21 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

Related Questions

Checking Transitions in Animator 0 Answers

How to setup this melee animation system in Mecanim? 1 Answer

Is it possible to play animator transition in script? 1 Answer

Transition to multiple states from Entry Node in Unity 5 1 Answer

Can't add more than one transtition to animator controller. 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