Problem with chasing ai

the code is working pretty well at first until the AI manage to catch the 1st mouse in the array it stop chasing and it give the error that said “the object have been destroy but you still try to access it” which is weird cuz if the other object in array get destroy it still work perfectly.

here is the code

public Transform mouses;

// Use this for initialization
void Start () {
    
}

// Update is called once per frame
void Update () {        
    Vector3 currentPos = transform.position;
    Transform tMin = null;
    float minDist = Mathf.Infinity;
    foreach (Transform mouse in mouses)
    {
        float dist = Vector3.Distance(mouse.position, currentPos);
        if (dist < minDist)
        {
            tMin = mouse;
            minDist = dist;
            transform.LookAt(tMin);
            transform.position += transform.forward * 3.5f * Time.deltaTime;
        }
        Debug.Log(tMin);
    }
}
private void OnTriggerEnter(Collider other)
{
    if (other.tag == "mouse")
    {
        Destroy(other.gameObject);
    }
}

As @GrKl said, you should use a list instead of an array, and use the condition if (mouse == null) to remove destroyed objects.

I modified your code :

    public List<Transform> mouses = new List<Transform>();

    void Update()
    {
        Vector3 currentPos = transform.position;
        Transform tMin = null;
        float minDist = Mathf.Infinity;

        List<Transform> destroyedMouses = new List<Transform>();
        foreach (Transform mouse in mouses)
        {
            if (mouse == null)
            {
                destroyedMouses.Add(mouse);
                continue;
            }

            float dist = Vector3.Distance(mouse.position, currentPos);
            if (dist < minDist)
            {
                tMin = mouse;
                minDist = dist;
                transform.LookAt(tMin);
                transform.position += transform.forward * 3.5f * Time.deltaTime;
            }
            Debug.Log(tMin);
        }

        foreach (Transform mouse in destroyedMouses)
            mouses.Remove(mouse);
        destroyedMouses.Clear();
    }

I added destroyedMouses list since you can’t edit mouses while in the loop, you would probably crash.