Why the scripting is not working in the StateMachineBehaviour class?

Hello.

I’m following the example provided in the Unity documentation about how to get and set parameters of the animator.

The script is inside of a State inside of a layer of the animator, and it is not compiling as it says.
UnassignedReferenceException: The variable animator of SnorHelmetRemove has not been assigned.
You probably need to assign the animator variable of the SnorHelmetRemove script in the inspector.
UnityEngine.Animator.GetBool (System.String name) (at <95298f2688224d229ba67639574988c7>:0)
SnorHelmetRemove.OnEnable ()

I am following the documentation. Why I am not getting the Animator?
This is the code.

public class SnorHelmetRemove : StateMachineBehaviour {
    private Animator animator;
    private int headSwitch;
    private int previousHeadSwitch;

    private void OnEnable() {
        animator = GetComponent<Animator>();
    }

    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        previousHeadSwitch = animator.GetInteger("HeadSwitch");
        animator.SetInteger("HeadSwitch", 0);
    }

    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if(previousHeadSwitch == 1)
        {
            animator.setInteger("HeadSwitch", 2);
        }
        else
        {
            animator.SetInteger("HeadSwitch", 1);
        }
    }
}

Have you debugged, why you dont get the Animator?

Does StateMachineBehavior inherit from Monobehavior? If it doesn’t it probably doesn’t call OnEnable.

Hello, it is in the line 8.

It can’t inherit both from Monobehaviour and StateMachineBehaviour, and it is in a StateMachine

I was asking if StateMachineBehavior was inheriting from it, not if your class inherited both. I had never seen StateMachineBehavior used before, but upon looking into it, it is a Unity made class. One of which, does not have a method called GetComponent, according to the documentation.