How to make a game object detect if any of multiple game objects are in range

I am currently working on an enemy in a multiplayer game. I want it to be able to detect if any of the players are in range of it and then move towards whichever player is closest. I am using the Mirror API for the multiplayer if that matters. With what I have already, the enemy will move towards one player, but once another player gets closer or the current player dies, the enemy will just stand still. Is there a more efficient way to do this or something I can fix?

What I have at the moment (I have this code in my update function):

int j = 0;
        foreach (GameObject player in players)
        {
            if (Vector3.Distance(players[j].transform.position, transform.position) <= followDistance && isStopped == false)
            {
                EnemyMove();
            }
            else
            {
                j++;
            }
        }

You could put them all into a collection, sort by distance then move towards the position of the first in the list. Possibly using look at and translate forwards?


When they should not be tracked then you could either remove them from the list or flag them and then you would just need to do a test i.e. not flagged and shortest distance

Add a big Sphere collider to your enemy and use the OnCollisionEnter/OnCollisionExit messages to add or remove targets from a list as @sacredgeometry suggests.