Search Unity

(SOLVED) How can I prevent " index was out of range" error on this script?

Discussion in 'Scripting' started by SiMULOiD, Mar 14, 2021.

  1. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    I'm trying to find a way to prevent an "index was out of range" error in update when there aren't any targets to find.
    I thought I could add an if statement to return if there weren't any "selected objects," but I'm still encountering the error.


    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class AI_FindObjects : MonoBehaviour {
    5.  
    6.      public List <Transform> Enemies;
    7.      public Transform SelectedTarget;
    8.  
    9.      void Update ()
    10.      {
    11.  
    12.          SelectedTarget = null;
    13.          Enemies = new List<Transform>();
    14.          AddEnemiesToList();
    15.      
    16. }
    17.      public void AddEnemiesToList()
    18.      {
    19.          GameObject[] ItemsInList = GameObject.FindGameObjectsWithTag("PitcherPlant");
    20.          foreach(GameObject _Enemy in ItemsInList)
    21.          {
    22.              AddTarget(_Enemy.transform);
    23.          }
    24.      }
    25.      public void AddTarget(Transform enemy)
    26.      {
    27.          Enemies.Add(enemy);
    28.      }
    29.      public void DistanceToTarget()
    30.      {
    31.          Enemies.Sort(delegate( Transform t1, Transform t2){
    32.              return Vector3.Distance(t1.transform.position,transform.position).CompareTo(Vector3.Distance(t2.transform.position,transform.position));
    33.          });
    34.      }
    35.      public void TargetedEnemy()
    36.      {
    37.          if(SelectedTarget == null)
    38.          {
    39.              DistanceToTarget();
    40.              SelectedTarget = Enemies[0];
    41.          }
    42.      }
    43.      void LateUpdate ()
    44.      {
    45.          TargetedEnemy();
    46.          float dist = Vector3.Distance(SelectedTarget.transform.position,transform.position);
    47.           transform.position = Vector3.Lerp (transform.position, SelectedTarget.position, Time.deltaTime*1);
    48.      }
    49. }
    50.  
    51.  
     
    Last edited: Mar 14, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236

    First, find where the out of range is happening.

    Second, find why

    Third, fix that.
     
  3. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Thanks, Kurt,
    I found where the error is coming from- which is the "Enemies" in the above list.
    And the why- there aren't any "Enemies" in the scene at runtime.
    What I'm not understanding is how to check if there are no Enemies, and prevent this exception error.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    The only dereference I see is line 40 above... you are hard-wiring and grabbing the first one (element zero).

    Don't do that if there isn't anything in the list. Collections such a list have a .Count property you can check to make sure it's greater than zero, and Arrays use the .Length property instead.
     
  5. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Something you can do to improve your script (as you're creating a new List every frame) is clear it instead:
    - Change line 6 to:
    public List<Transform> Enemies = new List<Transform>();

    - Change line 13 to:
    Enemies.Clear();
     
  6. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    And as Kurt mentioned, you can dodge the error in line 40 by changing line 37 to..
    if ((SelectedTarget == null) && (Enemies.Count != 0))

    ..and after line 45, you should add..
    if (SelectedTarget == null) return;
     
    Holocene likes this.
  7. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Thanks, Polemical- that sorted the issue out, grateful for the help.
    Also, thanks for showing me the reason behind this error, Kurt- much appreciated!
     
    Last edited: Mar 16, 2021
    adamgolden and Kurt-Dekker like this.