Animator SubState always plays Default even with a Behavior

Hey all,

Trying to set up a simple system that looks like this:

Animator Starts → Animator goes to SubState “idle” → A script fires to choose 1 of 6 random states → That state plays and loops.

Here’s what happens: Everything works, the script chooses the correct random state, but the Animator ignores my transition preferences and instead ALWAYS plays the default state that comes from Entry. What’s the solution here?
Animator:

Sample Transition:

if(stateIndex > x)…

OverrideCode:

public class randomSubStateManager : StateMachineBehaviour {
    public int numStates;

	 // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
	override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        int randoState = Random.Range(0, numStates);
        Debug.Log("controller chose rando index: " + randoState);
        animator.SetInteger("stateIndex", randoState);
    }

I have this problem too and cannot find solution, why no one answering on these questions?
In my case with just testing animator, even though states have conditions to exit state and go to another state machine, it loops back to default state. Not sure, kind of ignore conditions if go to exit state…

I just managed to make a smiliar setup work by throwing a default empty state into the sub-state machine and putting the randomizing behaviour on that default empty state.


I’m surprised that 4 years later it’s such a struggle to find a cookbook solution for such a trivial task. Shame on you, Unity.