Search Unity

Returning to a specific state using a public reference of State Machine Behavior

Discussion in 'Animation' started by tsantoro92, Mar 19, 2018.

  1. tsantoro92

    tsantoro92

    Joined:
    Aug 17, 2017
    Posts:
    2
    Hello,

    I'm using mecanim as a logical state controller and I ran into a situation where had I one branching state with three entry points, but wanted it to return to the previous state when finished. I couldn't find a way to ask for the previous state, so instead, I had the three entry point states send their fullPathHash to the branching state during their OnStateExit, and then have that branching state call Play.(fullPathHash) when finished.

    I sent the fullPathHash to the branching state using a public reference to its State Machine Behavior in the three entry point states, and then setting a public variable in the branching state. This gave me very inconsistent results. Sometimes the public variable would debug out as 0, sometimes the fullPathHash would debug out differently when it was sent from the entry states and when it was received in the branching state. When the branching state was finished, it would send me to a random state in the animator, sometimes a state that hasn't even been entered yet.

    I solved this by making the public variable of the fullPathHash in the branching state as static. Apparently as soon as the animator is turned on at runtime, it makes clones of all the state machine behaviors, so my public reference to the state machine behavior script wasn't working at all.

    Has anyone else run into a problem like this? Or do they know of a more proper way to for a state to return to the previous state? The way I'm doing it kinda feels like a work around.

    Thanks.

    Attached is picture of the animator. The "branching state" is Vaccines for Children, and the three entry states are the ones that have transitions to it.
    Screenshot (12).png