When/How does GetNextAnimatorStateInfo evaluate?

Hi, I have an enemy with a combo system which goes through a chain of animations. I want to make sure I know when the start/end of the animation is which works perfectly fine with the code below. I then need to make it so that if combo continues I have this script on every animation state with an attack on it. Presumably, the result is it enters the first attack in the chain, evaluates the next state name to see if the tag is “attack” and then by the end decides if should still continue the attack logic. The weird thing is, even if I make a simple node like attack 1 → attack 2 with no exit transition on attack 1 and both states have the script attached, it still reports that the ‘next state’ isnt tagged with “attack”, when it is. How can it not be if there is no alternative? Is there something I’m missing with how NextState works? Are the variables shared between all instances of the class because the base is a scriptable object? A bit confused to be honest.

public class EnemyAttackCheck : StateMachineBehaviour
{
    private EnemyCombat _enemyCombat;
    private bool _attackChain;

    public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        _enemyCombat = animator.gameObject.GetComponent<EnemyCombat>();
        _enemyCombat.InAttack(true);

        if (animator.GetNextAnimatorStateInfo(0).IsTag("attack"))
        {
            _attackChain = true;
        }
        else
        {
            _attackChain = false;
        }
       
    }

    public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {                 
        if (_attackChain == false)
        {
            Debug.Log("exit attack chain");
            _enemyCombat.InAttack(false);
        }       
    }   
}

Update: I solved this by just by separating it into 2 simple scripts so there isn’t even an need for comparing which I probably shouldve just done in the first place… but if anyone sees this I would still like to learn more about this class and why my approach didnt work. Thanks

Since this is one of the first things that pops up when looking for info about this ill redirect you to this answers post about the same thing: GetNextAnimatorStateInfo().taghash is always 0 - Questions & Answers - Unity Discussions

The jist of it is that GetNextAnimatorStateInfo only evaluates properly while a transition is happening, so you can’t use it in StateEnter/Exit since the transition is already over by that point.

Further more if you have a transition duration of 0 it also won’t return anything, since I guess there’s no transition happening? Very frustrating to try and use when needing instant transitions as opposed to blending between animations…