Search Unity

GetCurrentAnimatorStateInfo Issue with triggers [SOLVED]

Discussion in 'Scripting' started by magique, Feb 14, 2021.

  1. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    I'm trying to get the state of an Animator Trigger so I can save and restore it. I found the following documentation, which uses a Trigger named Jump:

    https://docs.unity3d.com/ScriptReference/Animator.GetCurrentAnimatorStateInfo.html

    The example suggest using the following code to check the trigger named "Jump":

    Code (CSharp):
    1.         if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Jump"))
    2.         {
    3.             Debug.Log("Jumping");
    4.         }
    5.  
    In my game, I have a Play and Stop trigger. When the Play trigger is set then it transitions from Idle to Play. The actual Animator I'm using is working fine and when I call SetTrigger("Play") it transitions exactly as it should. However, not only don't I see the named trigger in the Animator editor turned on, when I call the following code, it returns false:

    Code (CSharp):
    1. if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Play"))
    I know the transition is occurring on the trigger, but this code fails to return true. I can't find any examples anywhere that are doing it any different and no one else seems to have a problem as far as I can tell. Is there something obvious I might be missing?

    I'm using Unity 2019.4.15f1.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,688
    Are you calling that from within one of these callbacks?

    https://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html

    You might need that because of its various OnStateEnter(), OnStateExit(), etc. and the trigger might only be valid in one.

    Disclaimer: I'm only speculating, but I've used other Unity APIs shaped roughly like this.
     
  3. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    No, I didn't do anything like that. The API documentation I referred to didn't have anything like that in there and yet they seem to be indicating the example class would work exactly as written. So, I'm very skeptical that anything else would be needed. I also didn't see anything like that in other examples others have posted. It would also be useless to me if I had to go through the other code because the class that needs the information isn't monitoring the Animator in real-time and is only there to save the state on game save and restore it on game load.

    I'm beginning to think it's a bug in this version, but I'll keep investigating. Thanks for responding and giving it a shot.
     
  4. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    OK, so I've gone ahead and copied the Example class provided in the documentation and it works just fine. So, it only isn't working in my own code. I'll see if I can figure out what is different that would make it fail for my case.
     
  5. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Hmmm. I spoke too soon. My initial test I added 2 animations to the controller, but didn't have an actual animation clip. That works fine. However, as soon as I actually use 2 states with real animations then it no longer reports the state as Jumping even though the animation trigger is occurring and the animations are correct.
     
  6. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Too funny. I stripped it back to no motions and it still failed. Then I recreated it with no motions and it worked and then added motions and it still works. Even though it didn't work last time. I don't get it. I'll try with my original object and animator now and see what happens.
     
  7. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    OK, so I recreated my Animator by making empty states with no motions and hooking up the triggers. Then I add the motions after that then everything works the way it's supposed to. This has to be some sort of weird bug. But at least I have a way to fix it now.
     
    sstrong and Kurt-Dekker like this.