how to find the farest enemy / game objects with tag?

hi!
if i want to find the closest enemy the unity script reference says this:

// Print the name of the closest enemy
print(FindClosestEnemy().name);

// Find the name of the closest enemy
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var gos : GameObject;
gos = GameObject.FindGameObjectsWithTag(“Enemy”);
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}

but what would the code be for finding the farest enemy?
thanks a lot!

I would recommend iterating through all the enemies in the scene:

int closest = 0;
float closestEnemy = 0f;
Enemy[] enemies = GameObject.FindObjectsOfType(typeof(Enemy));
for(int i = 0; i < enemies.Length; i++)
{
  float distance = (myHero.transform.position - enemy*.transform.position).magnitude;*

if(distance > closestEnemy)
{
closest = i;
}
}
I didn’t check that this compiles, but you get the idea. You could use the same with a less-than to find the farthest. Hope that helps!