title: Is my target selection AI efficient?
body: <p>quick question. I am developing a top-down 2d Platformer game with lots of enemies in the map (at least a hundred spawn at the start of each level). Each enemy uses an AI that searches the map for objects with a specified tag, sorts each object into a list based on their distance, then reacts to the object closest to them.</p>
  
  <p>My code works, but the thing is, if the machine my game is running on is slow, then my game lags. I want to be able to port my game to Android and iOS with low end specs.</p>
  
  <p>In pursuit of putting less strain on the CPU, is there a better way to write my AI?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>void Start () {
  
      FoodTargets = new List&lt;Transform&gt;(); // my list
      SelectedTarget = null; // the target the enemy reacts to
      myTransform = transform; 
      AddAllFood ();
  
  }
  
  public void AddAllFood()
  {
  
      GameObject[] Foods = GameObject.FindGameObjectsWithTag("Object");
      foreach (GameObject enemy in Foods)
          AddTarget (enemy.transform);
  
  }
  
  public void AddTarget(Transform enemy)
  {
      if (enemy.GetComponent&lt;ClassRatingScript&gt;().classrating != 1) { // classrating is an attribute each enemy has that determines their identity (like if they are a plant, a herbivore or a carnivore)
          FoodTargets.Add (enemy); // adds the object to the list
      }
  }
  
  
  private void SortTargetsByDistance() // this is how I sort according to distance, is this the fastest and most efficient way to do this?
  {
      FoodTargets.Sort (delegate(Transform t1, Transform t2) { 
          return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
      });
  }
  
  private void TargetEnemy() // this is called every 4 frames
  {
      if (SelectedTarget == null) {
          SortTargetsByDistance ();
          SelectedTarget = FoodTargets [1];
      } 
      else {
          SortTargetsByDistance ();
          SelectedTarget = FoodTargets [1];
      }
  }
  
                  if (optimizer &lt;= 2) { // this is a variable that increments every frame and resets to 0 on the 3rd frame. Only every 3rd frame is the target enemy method is called.
                      optimizer++;
                  } else {
                      TargetEnemy ();
  
  // the rest are attributes that the AI considers when reacting to their target
  
                      targetmass = SelectedTarget.GetComponent&lt;MassScript&gt; ().mass;
                      targetclass = SelectedTarget.GetComponent&lt;ClassRatingScript&gt; ().classrating;
                      mass = this.GetComponent&lt;MassScript&gt; ().mass;
                      classrating = this.GetComponent&lt;ClassRatingScript&gt; ().classrating;
                      distance = Vector3.Distance (transform.position, SelectedTarget.transform.position);
  
                      optimizer = 0;
                  }
  </code></pre>
  
  <p>Is there a more optimized way of doing this? Your help will be much appreciated. Thanks in advance!</p>