Skip to content

Instantly share code, notes, and snippets.

@unity3dcollege
Created July 1, 2017 20:00
Show Gist options
  • Save unity3dcollege/060dd81a97e53a53a48013270432dbce to your computer and use it in GitHub Desktop.
Save unity3dcollege/060dd81a97e53a53a48013270432dbce to your computer and use it in GitHub Desktop.
private GameObject GetNearestGameObject(List<GameObject> gameObjectsToConsider)
{
float smallestDistance = Mathf.Infinity;
GameObject nearestGameObject;
foreach(var go in gameObjectsToConsider)
{
var distance = Vector3.Distance(transform.position, go.transform.position);
if (distance < smallestDistance)
{
smallestDistance = distance;
nearestGameObject = go;
}
}
return nearestGameObject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment