Search Unity

Resolved Animator.SetBool is working the first time, but not the second time?

Discussion in 'Animation' started by SpatialEffects, Sep 21, 2020.

  1. SpatialEffects

    SpatialEffects

    Joined:
    Jan 13, 2020
    Posts:
    10
    Hey guys, I'm making a project for a Hololens 2. I've got a simple character with two states in the animator: Idle and walking. The Idle state can transition to walking if the isWalking parameter is set to true, and the walking state can transition to idle if the isWalking parameter is set to false.

    When I click the checkbox manually in the Animator window when my project is running, it works.

    The first time when I set "isWalking" via code, it also works. But then when I set isWalking to false, nothing happens, worse, it seems like the method is throwing an invisible exception because it doesn't even hit the "Done setting walking" flag! WTF??

    I'm on Unity 2019.4.10 LTS

    Code (CSharp):
    1. private void SetCharacterWalking(bool walking)
    2.     {
    3.         Debug.Log($"Character walk state set: {walking}");
    4.         CharacterAnimator.SetBool("isWalking", walking);
    5.         Debug.Log("Done setting walking flag");
    6.     }
     
  2. SpatialEffects

    SpatialEffects

    Joined:
    Jan 13, 2020
    Posts:
    10
    OK, fixed it, it was a thread issue. I was using a System.Timers.Timer to trigger the method. Now I'm just using the timer to set a private bool and do the work in the update method.
     
  3. SpatialEffects

    SpatialEffects

    Joined:
    Jan 13, 2020
    Posts:
    10
    Screw that, I'm just ditching System.Timers.Timer altogether, I remember having a lot of issues with it in Unity in the past. Switched to coroutines to make a simple timer.