Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created January 4, 2015 21:41
Show Gist options
  • Save unitycoder/9a67e9bd559fb622d0e2 to your computer and use it in GitHub Desktop.
Save unitycoder/9a67e9bd559fb622d0e2 to your computer and use it in GitHub Desktop.
LINQ: Pick closest transform from list of gameobjects
using System.Linq;
..
// get list of objects with tag "Player"
GameObject[] gos = GameObject.FindGameObjectsWithTag("Player");
// get closest transform from gos[] array, into target variable, as transform object
var target = gos.OrderBy(go => (transform.position - go.transform.position).sqrMagnitude).First().transform;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment