Getting a state from the Animator

Here is the script I am using to set the name hash:
var cState : int;
cState = Animator.StringToHash(“Base Layer.Idle”);

Now the hard part is to actually do something with this. All I want is to do something when this animation starts playing. I looked at some questions and this was one of the more helpful answers: unity game engine - Get Animation states from an Animator in Unity3d - Stack Overflow.

So far, everything I have looked at tells me I need to use GetCurrentAnimatorStateInfo(). However, I just get the error "An instance of type ‘UnityEngine.Animator’ is required to access non static member ‘GetCurrentAnimatorStateInfo’. The docs show Animator.GetCurrentAnimatorStateInfo, but all that does is give me the error as stated before(I’m probably using it wrong).

So if you have any information on what I should rework or what I’m doing wrong, let me know. All I’m trying to do is get the name of the clip in the Animator that is playing.

here’s an example script which might answer your question(assuming you’re writing in UnityScript)

#pragma strict

var anim : Animator;
var jumpHash : int = Animator.StringToHash("Jump");
var runStateHash : int = Animator.StringToHash("Base Layer.Run");


function Start () 
{
    anim = GetComponent("Animator");
}


function Update () 
{
    var move : float = Input.GetAxis ("Vertical");
    anim.SetFloat("Speed", move);

    var stateInfo : AnimatorStateInfo = anim.GetCurrentAnimatorStateInfo(0);
    if(Input.GetKeyDown(KeyCode.Space) && stateInfo.nameHash == runStateHash)
    {
        anim.SetTrigger (jumpHash);
    }
}

for more info,please visit this link:
http://unity3d.com/learn/tutorials/modules/beginner/animation/animator-scripting

The error means you are using the GetCurrentAnimatorStateInfo with no instance of Animator class. As this is not a static method, you should use it by some Animator object but not the Animator class directlt.

in the Start() of your gameobject that has an Animator component, add this:

Animator animatorOfThisObj = this.GetComponent();

print(animatorOfThisObj.GetCurrentAnimatorStateInfo);

you should be able to get this now, will attach a better script when i have my mac

Two blend trees in your mechanim; I used one for walking routines, and another for running routines. Three variables are set: speedZ, speedX floats which store the values from the walking and running blend trees. Then isRunning bool which stores the value if my character is running or not. I also have an input left/right shift for running setup as “Run” which correlates with the logic you’ll see below. This is a simple example of using StringToHash.

    private Animator anim;
    int isRunning = Animator.StringToHash("isRunning");
    public static P_Animator Instance { get; set; }

    protected void Awake()
    {
        Instance = this;
        anim = GetComponent<Animator>();
    }

    protected void Update()
    {
        float moveV = Input.GetAxis("Vertical");
        anim.SetFloat("speedZ", moveV);
        float moveH = Input.GetAxis("Horizontal");
        anim.SetFloat("speedX", moveH);
        AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);


        if (P_Controller.CharacterController.isGrounded)
        {
            if (Input.GetButton("Run"))
            {
                anim.SetBool(isRunning, true);
            }
            else
            {
                anim.SetBool(isRunning, false);
            }
        }
    }

Hi,

your int cState variable looks good I would say, you can simply do this:

int cState = Animator.StringToHash("Base Layer.Idle");
if (_animator.GetCurrentAnimatorStateInfo(0).fullPathHash == cState){ 
 //do something
}

(_animator.GetCurrentAnimatorStateInfo(0) gets the information of the animation state currently playing)

But you don’t have an instance of your animator, attach you animator controller to the gameObject that has the script in which you want to access the animator:

As global variable define:

public Animator _animator;

Then in the Start() or Awake() function you actually have to get the instance of the animator, else it remains null and you cannot invoke any function on it:

_animator = GetComponent<Animator>();

Hope this helps.