title: Getting ArgumentOutOfRange exception, even though I constrained the value of the supplied index
body: <p>I'm working on a script where the player can toggle between certain objects. If the player touches the object then it is destroyed. I have all the objects in a list that update every second to check if something leaves or is added to said list. I'm having problems dealing out of range errors at the moment and was wondering if anyone could help. How do I properly check to see if index item in list is null or invalid without the error messages?</p>
  
  <p>Here's the error:</p>
  
  <blockquote>
    <p>ArgumentOutOfRangeException: Argument is out of range.
    Parameter name: index
    System.Collections.Generic.List`1[UnityEngine.GameObject].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
    TargetEnemy.SwitchTarget () (at Assets/TargetEnemy.cs:39)
    TargetEnemy.Update () (at Assets/TargetEnemy.cs:33)</p>
  </blockquote>
  
  <p>Here's my script:</p>
  
  <pre><code>public int selectableTargets;
  public int currentTarget;
  public Rigidbody rb;
  public bool lockedOn;
  
  void Start ()
  {
      selectableTargets = LocateEnemy.enemyCount;
      currentTarget = 500;
      rb = GetComponent&lt;Rigidbody&gt;();
      lockedOn = false;
  }
  
  // Update is called once per frame
  void Update ()
  {
  
      selectableTargets = LocateEnemy.enemyCount;
      if (currentTarget &gt; selectableTargets || currentTarget &lt; 0)
      {
          print("I cant find it");
      }
      else
          SwitchTarget();
  
  }
  
  void SwitchTarget()
  {
      if(LocateEnemy.myTargets[currentTarget].gameObject != null)
        rb.transform.LookAt(LocateEnemy.myTargets[currentTarget].gameObject.transform);
  
  }
  </code></pre>