# How to find the first two GameObjects nearest to another?

Trying to find the two closest targets nearest to another gameobject.

Below is what I was thinking something, but doesn’t quite work with finding both.

``````  GameObject[] targets = GameObject.FindGameObjectsWithTag("Player");
GameObject nearest = null;
GameObject secondNearest = null;
firstClosest = Mathf.Infinity;
nextClosetDistance = Mathf.Infinity;

foreach (GameObject obj in targets)
{
float d = Vector3.Distance(transform.position, obj.transform.position);

if (d < firstClosest)
{
if (firstClosest < nextClosetDistance)
{
nextClosetDistance = firstClosest;
secondNearest = obj;
}
firstClosest = d;
nearest = obj;

}
if (d < nextClosetDistance)
{
nextClosetDistance = d;
secondNearest = obj;
}
}
``````

Looking at your code, it looks like you might be setting the second nearest to the same as the first?

You should be able to simplify your comparison with

``````            if (d < firstClosest)
{
secondNearest = nearest;
nearest = obj;

nextClosestDistance = firstClosest;
firstClosest = d;
}
``````

If you only have one object, the second object will be set to null and its distance will be set to infinity.

If you are looking to optimize this, instead of using Vector3.Distance, use sqrMagnitude and compare squared distances. Squaring is much cheaper than square roots. You can take the root of the final magnitudes if you need to be able to use them.

So does it find 1, or 2? Or Neither. Debug your values, see whats coming, whats working whats not etc, then work from there

you are going to need to iterate very object anyways so might be best to just use OrderBy.

``````var targets = GameObject.FindGameObjectsWithTag("Player");
var twoClosest = targets.OrderBy(x => (transform.position - x.transform.position).sqrMagnitude).Take(2).ToList();
``````

for this to work you will need to include System.Linq at the top of the file

``````using System.Linq;
``````

am comparing sqrMagnitude instead of distance since its a cheaper operation and in your case you do not care about the real distance just need to no which has a smaller distance.

Hello everyone,

Thanks for the replies.

I got it to work with this:

`````` if (d < firstClosest)
{
secondNearest = nearest;
nearest = obj;

nextClosestDistance = firstClosest;
firstClosest = d;
}
``````

I actually had a problem else where, that was stopping it from originally working as I was also trying to move on a grid and the moving wasn’t working with the targeting before finding the distance. Figured it out after debugging.

Now I’m having an issue with telling if there are two objects that are the same distance away.

For example:
I have a “Unit” and on both sides of the unit are objects (enemies) that are both two squares away.
The code now only grabs one enemy object rather then two since it detects that both are the same distance.

How would I find all objects that are the same distance away, then add them too nearest and secondNearest?

Solved