title: What is the most effective way to get closest target
body: <p>What is the most effective and less expensive way to get closest target from these two methods?</p>
  
  <p>Using LINQ</p>
  
  <pre><code>GameObject FindClosestTarget(string trgt) 
  {
      GameObject[] closestGameObject = GameObject.FindGameObjectsWithTag(trgt)
                        .OrderBy(go =&gt; Vector3.Distance(go.transform.position, transform.position)
                        .FirstOrDefault();
           return closestGameObject ;
  }
  </code></pre>
  
  <p>or this</p>
  
  <pre><code> GameObject FindClosestTarget(string trgt) 
       {
           GameObject[] gos= GameObject.FindGameObjectsWithTag(trgt);
  
           GameObject closest=null;
           float distance = Mathf.Infinity;
           Vector3 position = transform.position;
           foreach (GameObject go in gos) {
               Vector3 diff = go.transform.position - position;
               float curDistance = diff.sqrMagnitude;
  
               if (curDistance &lt; distance) {
                   closest = go;
                   distance = curDistance;
               }
           }
  
           return closest;
       }
  </code></pre>