Search Unity

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

Discussion in 'Scripting' started by Izzow, Jul 31, 2018.

  1. Izzow

    Izzow

    Joined:
    Dec 17, 2017
    Posts:
    4
    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:

    Code (CSharp):
    1. AnimatorStateInfo curr_state = _animator.GetCurrentAnimatorStateInfo(0);
    2. if (curr_state.shortNameHash == Animator.StringToHash("ThoughtsStill"))
    3. {
    4.     Debug.Log("I'm in ThoughtsStill state").
    5. }
    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 !
     
    alexr1221 likes this.
  2. Izzow

    Izzow

    Joined:
    Dec 17, 2017
    Posts:
    4
    Still can't find any solutions, does someone know something about this?
     
    akuno likes this.
  3. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    451
    What is not working exactly? The comparison? Getting the StateInfo?
     
  4. DarkGate

    DarkGate

    Joined:
    Jan 26, 2016
    Posts:
    33
    Having the same issue, cant believe this is still a thing lol. Someone from the Unity team need to make this more usable.
     
  5. alexr1221

    alexr1221

    Joined:
    Mar 5, 2016
    Posts:
    70
    Still here Unity 2021.3.23f1
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    I'm not sure what the problem is.
    I haven't touched Animator in years, but after glancing the docs it's obvious that this should work like this
    Code (csharp):
    1. var state = _animator.GetCurrentAnimatorStateInfo(0);
    2. if(state.IsName("Base.ThoughtsStill")) {
    3.   Debug.Log("I'm in ThoughtsStill state");
    4. }
    I mean just read what is written.
    I am not referring to the OP btw, but OP is the only one who at least bothered with an example.
     
  7. alexr1221

    alexr1221

    Joined:
    Mar 5, 2016
    Posts:
    70
    Referring to the OP example :
    Code (CSharp):
    1. curr_state.shortNameHash == Animator.StringToHash("ThoughtsStill")
    should be indeed equal.

    When using
    Code (CSharp):
    1. _animator.HasState(0, Animator.StringToHash("ThoughtsStill"))
    it will return true, so shortNameHash doesn't refer to ThoughtsStill nor fullPathName to Base Layer.ThoughtsStill

    OP :
    your test was successful?
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    Can't you appreciate the difference between the following strings

    "ThoughtsStill" [OG]
    "Base Layer.ThoughtsStill" [OP's version]
    "Base.ThoughtsStill" [what should've been]

    ?

    It wasn't because OP tried "Base Layer.ThoughtsStill".

    Besides, this thread is 5 years old, I doubt you'll get a reply from OP.