Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why does OnStateExit run 2 times?

Discussion in 'Animation' started by jwinn, Jul 24, 2016.

  1. jwinn

    jwinn

    Joined:
    Sep 1, 2012
    Posts:
    88
    I put a Debug.Log in my StateMachineBehaviour's OnStateExit function, and was surprised to find out that it prints out sometimes 2 times, and sometimes once. I can't find any documentation to explain this unexpected behavior. It says that it should only be called on the last frame.

    Something to do with it being a blend tree? If so, why would it not be treated as a whole?

    I did a Debug.Break() and skipped frames to try and see where they were occurring. I think the first is as the transition starts, and the second is after the transition is over. Does this mean that it will always sometimes run twice and I need to check that IsInTransition is false to make sure it's really on a complete exit of the state? Not sure why it differs between once and twice though.
    EDIT: Problem still occurs if I use "if (!animator.IsInTransition(0))"

    state_exit.jpg
    The OnStateExit is on the "Falling" state. It is transitioning to the Grounded sub-state machine. I tried muting the Landing and Rope transitions just in case, with the same result.
     
    Last edited: Jul 26, 2016
  2. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    If you have a use case where this happens, please log a bug and paste the number here.

    This is not supposed to happen, unless you are reentering your state
     
  3. jwinn

    jwinn

    Joined:
    Sep 1, 2012
    Posts:
    88
    It's not a bug after all, but thanks!

    I just figured it out after much debugging and looking at it frame by frame.
    It was the Any State transition to falling based on a boolean, and the state was being reentered. I ended up adding another "StartFalling" trigger parameter. It was hard to tell what the path in mecanim was visually.