# Can't get the current animation state name (hash or .IsName) to work

I have some hard times trying to get the current animation state name in Unity, to perform specific actions while I'm in a certain state. I want to know when I'm the "ThoughtsStill" state, I only have the default layer (Base Layer):

To do this, here is what I tried:

AnimatorStateInfo curr_state = _animator.GetCurrentAnimatorStateInfo(0);
if (curr_state.shortNameHash == Animator.StringToHash("ThoughtsStill"))
{
Debug.Log("I'm in ThoughtsStill state").
}


which is not working.

I also tried with IsName() method but it is not working too (both tried to compare "Base Layer.ThoughtsStill" and "ThoughtsStill").

I'm certain that I'm going in this ThoughtsStill state but I don't know why I can't get it.

Do you guys have any ideas or leads?

Thanks !

You should try debugging by printing out the hashed name of the current state and print out the hashed string. Just read from the console and see the difference. If they are different you might not be at all going through that state. Printing them out is always the best thing to do in those situations.

If they are not equal, try testing with other names and see when they become equal. That way you will be able to discover which states you are going through. You might not be going through that state at all.

Also try using nameHash instead of shortNameHash and then just use Base Layer.ThoughtsStill as you said you tried with isName method.

Or, Unity maybe doesn't want you to type "Base Layer.ThoughtsStill", but it maybe wants you to type "BaseLayer.ThoughtsStill" without a space. I'm not sure about that but just try it out.

Also NEVER convert your string to hash every single time in a loop. Just make a readonly variable at the top of the class and use it every time because this is expensive.

I can confirm that you do need to include the space in the full name, so "Base Layer.ThoughtsStill" is the correct name to use and I always use fullPathHash instead of shortNameHash. Also, you might want to look into State Machine Behaviours: https://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html

Using State Machine Behaviours it's much easier to catch when a state starts/updates/exists and catch it exactly the moment it triggers. So my recommendation would be to:

1) Cache the generated hash code

stateHashCached = Animator.StringToHash("Base Layer.ThoughtsStill"))


2) Implement a StateMachineBehaviour, attach it to your animator's Base Layer and listen for OnStateEnter.

3) Do your compare inside the OnStateEnter like this:

if (stateHashCached ==  curr_state.fullPathHash)


We can get the current playing clip name in the layer using following code

private Animator animator = GetComponent<Animator>();
private AnimatorClipInfo[] clipInfo;

public string GetCurrentClipName(){
clipInfo = animator.GetCurrentAnimatorClipInfo(0);
return clipInfo[0].clip.name;
}


Note : This code is tested in unity version 2018.3 so I am not sure if this works on previous version or not.