get closest enemy

hey, how do I get a list of all the gameObjects in my scene with the name 'enemy' and then which one is the closest to the object with this script attached to it. thanks

Rather than find ones with the name Enemy on them, you should either search for objects with a particular script on them, or for objects with a particular tag on them.

I recommend tags, because it's probably much faster, and an example of finding the closest enemy is actually given in the manual entry for FindGameObjectsWithTag. The function you want is FindClosestEnemy. A minor change I'd make: in the example, I see they are calculating distance with two lines of code:

var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude; 

This can be replaced with one line:

var curDistance = Vector3.Distance(go.transform.position - position);

If you need to find the closest enemy often, an alternative might be to put a big spherical collider (set to be a trigger) on the object. Then (assuming the enemies have colliders on them) you will get notified as soon as an enemy moves into range.