Search Unity

Is the Animator Component Globally accessible?

Discussion in 'Scripting' started by bscarl88, Jul 10, 2015.

  1. bscarl88

    bscarl88

    Joined:
    May 11, 2012
    Posts:
    39
    I'm following project stealth tutorial at chapter 203. In the following script (attached to a GameManager in Unity), why doesn't the Animator have to be set to a variable using GetComponent<Animator>() ? This scipt isn't even tied to the player controller to access it directly. Is Animator a globally accessible component by default, and Animator Controllers are like an individual instance of Animator?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HashIDs : MonoBehaviour
    5. {
    6.     // Here we store the hash tags for various strings used in our animators.
    7.     public int dyingState;
    8.     public int locomotionState;
    9.     public int shoutState;
    10.     public int deadBool;
    11.     public int speedFloat;
    12.     public int sneakingBool;
    13.     public int shoutingBool;
    14.     public int playerInSightBool;
    15.     public int shotFloat;
    16.     public int aimWeightFloat;
    17.     public int angularSpeedFloat;
    18.     public int openBool;
    19.    
    20.    
    21.     void Awake ()
    22.     {
    23.         dyingState = Animator.StringToHash("Base Layer.Dying");
    24.         locomotionState = Animator.StringToHash("Base Layer.Locomotion");
    25.         shoutState = Animator.StringToHash("Shouting.Shout");
    26.         deadBool = Animator.StringToHash("Dead");
    27.         speedFloat = Animator.StringToHash("Speed");
    28.         sneakingBool = Animator.StringToHash("Sneaking");
    29.         shoutingBool = Animator.StringToHash("Shouting");
    30.         playerInSightBool = Animator.StringToHash("PlayerInSight");
    31.         shotFloat = Animator.StringToHash("Shot");
    32.         aimWeightFloat = Animator.StringToHash("AimWeight");
    33.         angularSpeedFloat = Animator.StringToHash("AngularSpeed");
    34.         openBool = Animator.StringToHash("Open");
    35.     }
    36. }
     
    Last edited: Jul 10, 2015
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    In Unity 4.X (which I assume is what you are using) when they introduced Animator, they made it an inherited property. Basically it looks something like this


    Code (CSharp):
    1.     public Animator animator {
    2.         get {
    3.             if(this.m_animator == null) {
    4.                 this.m_animator = this.GetComponent<Animator>();
    5.             }
    6.  
    7.             return this.m_animator;
    8.         }
    9.     }
    In Unity5, however, Rigidbody, Collider, Animator and some other inherited properties have now been deprecated. You now have to store a reference to them yourself as not all GameObject components will use these properties. Hence why in the tutorial, you might not see a variable for it but its part of GameObject. Again in Unity5 you have to cache the reference yourself.
     
  3. bscarl88

    bscarl88

    Joined:
    May 11, 2012
    Posts:
    39
    (I added the code that I forgot to add above to my original post)

    That part I get, that Unity 5 components have to have a stored reference to be able to use them, so Ive changed my scripts to do so. However, doing the tutorial in Unity 5, I didnt have to store the animator as a variable, it was able to access it directly with the code above without having to use GameObject.FindObjectWithTag("Player").getComponent<Animator>(); Which surprised me. I was wondering why I don't have to store the reference to a variable.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Animator.StringToHash() is a static function (like Vector3.MoveTowards(), Mathf.Lerp() and hundreds of other functions). You don't need an instance to invoke the method.
     
  5. bscarl88

    bscarl88

    Joined:
    May 11, 2012
    Posts:
    39
    ah ok, that makes sense. So just to get a complete understanding... Does that mean that Animator (as a static function) can reach out to all the AnimatorController's animations to find a specific animation like this: Animator.StringToHash("Base Layer.Dying"); is this finding the "Dying" animation on an animatino controller?

    OR is that not actually going to the base layer of a specific AnimatorController to find that Animation, it's just taking that general string and turning it into a HashID?
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It's better on all accounts to just cache a reference to Animator component and use that.
     
    Polymorphik likes this.
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Animator isn't static. Animator.StringToHash is. Check out the documentation to see what's static and what isn't. Animator.StringToHash has zero information about any specific Animator or AnimatorController instance. It's just there to generate hashes for strings (as comparing integers is much faster than comparing strings). You're spot on with "it's just taking that general string and turning it into a HashID".
     
  8. bscarl88

    bscarl88

    Joined:
    May 11, 2012
    Posts:
    39
    thanks everyone! I'm really starting to understand all of this, and couldn't have done it without such a wonderful community :)
     
    Polymorphik likes this.