I don't know how to make the script detect an animation

Hi, I’m making a video game and I’m having trouble with the script for the first boss. I tried to make the boss not move until the script detects that the animation is playing, but I’m not really sure how. For some reason I get an error on line 41:

“error CS1061: ‘GameObject’ does not contain a definition for ‘animator’ and no accessible extension method ‘animator’ accepting a first argument of type ‘GameObject’ could be found (are you missing a using directive or an assembly reference?)”

The code is a mess, but it works except for that error, here it is:
.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class GigaChad : MonoBehaviour
{
private bool checkPlayer = false;
public Transform target;
public GameObject Boss;
public float CoolDown = 3.0f;
public bool CanAttack = true;
public int digit;
NavMeshAgent nav;

void Start()
{
    Animator anim = Boss.GetComponent<Animator>();
    nav = GetComponent<NavMeshAgent>();
}

void Update()
{
    if (checkPlayer == true)
    {
        if (CanAttack == true)
        {
            digit = Random.Range(0, 101);
            if (digit <= 80)
            {
                Punch();
                digit = Random.Range(0, 101);
            }
            if (digit >= 81)
            {
                Combo();
                digit = Random.Range(0, 101);
            }
        }
    }
    if (Boss.animator.GetCurrentAnimatorStateInfo(0).IsName("Punch"))
    {
        nav.SetDestination(target.position);
        GetComponent<NavMeshAgent>().speed = 100f;
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.tag == "player")
    {
        checkPlayer = true;
    }
}

void Punch()
{
    CanAttack = false;
    Animator anim = Boss.GetComponent<Animator>();
    anim.SetTrigger("IsAttack");
    CanAttack = true;
    GetComponent<NavMeshAgent>().speed = 0f;
}

void Combo()
{
    CanAttack = false;
    GetComponent<NavMeshAgent>().speed = 10f;
    Animator anim = Boss.GetComponent<Animator>();
    anim.SetTrigger("IsCombo");
    nav.SetDestination(target.position);
    GetComponent<NavMeshAgent>().speed = 100f;
    CanAttack = true;
}

void Ultimate()
{
    CanAttack = false;
    GetComponent<NavMeshAgent>().speed = 100f;
    Animator anim = Boss.GetComponent<Animator>();
    anim.SetTrigger("IsUltimated");
    StartCoroutine(ResetAttackCoolDown());
}

IEnumerator ResetAttackCoolDown()
{
    yield return new WaitForSeconds(CoolDown);
    CanAttack = true;
}

}

So if anyone knows how to fix that, I’d be grateful.

Also in the code, I’m not currently using the ResetAttackCoolDown and void Ultimate, so don’t look at that

Also the script is showed weirdly in this post so as you look at it, the error is not on line 41, it’s on line 26

For one, I see that you keep declaring Animator anim = Boss.GetComponent<Animator>(); , you only need to do this once, and never while in runtime(severely impacts performance). Which actually you do almost cache it already, it’s in your start function. So do the same for Animator as you do navMesh, and you’ll only need to call anim in those cases. Also don’t need to keep calling navmesh for speed either, as it is already cached in Start().

Actually what is easier, is to just make initial public declarations of them :

public Animator anim;
public NavMeshAgent nav;

And then you won’t need to cache the reference, since you will click and drag them into the inspector(Anim and Nav spot appropriately). You only need to GetComponent() on things that you don’t set, and must find while in game. Which if you must do, do very sparingly(severe drop on performance).