I'm finding closest with GameObject.FindGameObjectsWithTag("Enemy"). How can I use this code (or anything else) to find nearest enemy game object in multiplayer (unet)?

I’m finding closest with GameObject.FindGameObjectsWithTag(“Enemy”). How can I use this code (or anything else) to find nearest enemy game object in multiplayer (unet)? I am actually making an rts game? Your help would be appreciated as I am new to the multiplayer game. Thank you.

  // Find the name of the closest enemy
    
    GameObject FindClosestEnemy()
    {
        GameObject[] gos;
        gos = GameObject.FindGameObjectsWithTag("Enemy");
        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
        foreach (GameObject go in gos)
        {
            Vector3 diff = go.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = go;
                distance = curDistance;
            }
        }
        return closest;
    }
    }

function Update()
{
Debug.Log("Closest Object is " + GetClosestObject(“tagName”).name);
}

 function GetClosestObject(tag:String) : GameObject
 {
     var objectsWithTag = GameObject.FindGameObjectsWithTag(tag);
     var closestObject : GameObject;
     for (var obj : GameObject in objectsWithTag)
     {
         if(!closestObject)
         {
            closestObject = obj;
         }
         //compares distances
         if(Vector3.Distance(transform.position, obj.transform.position) <= Vector3.Distance(transform.position, closestObject.transform.position))
         {
            closestObject = obj;
         }
     }
     return closestObject;
 }

I got this a few years ago. Hope this helps