Search Unity

Fixing the ArgumentOutOfRangeException: Argument is out of range. Parameter name: index

Discussion in 'Scripting' started by MikawasakiDigital, Sep 20, 2019.

  1. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Hi again everyone. I have this error appear when my games calls a specific if statement. i did research online and iv'e been able to muster up that it keeps pulling a negative number from from a list of random attacks I have which causes a crash sometimes. would anyone know how to resolve this?

    Also, what I'm trying to do here is if the attack that is chosen at random is "Ranged" it does one "if" statement.

    Below I'll leave the section of code that this corresponds to.
    Code (CSharp):
    1.   private IEnumerator TimeForAction()
    2.     {
    3.         if (actionStarted)
    4.         {
    5.             yield break;
    6.         }
    7.         actionStarted = true;
    8.  
    9.         if (CSM.PerformList[0].chosenAbility.aniStance == "Hit")
    10.         {
    11.             Vector3 playerPosition = new Vector3(PlayerToEngage.transform.position.x, PlayerToEngage.transform.position.y, PlayerToEngage.transform.position.z - 1.5f);
    12.             while (MoveTowardsPlayer(playerPosition))
    13.             {
    14.                 yield return null;
    15.             }
    16.             yield return new WaitForSeconds(0.5f);
    17.             DoDamage();
    18.             Vector3 firstPosition = startPosition;
    19.             while (MoveTowardsStart(firstPosition))
    20.             {
    21.                 yield return null;
    22.             }
    23.         }
    24.  
    25.         if (CSM.PerformList[0].chosenAbility.aniStance == "Ranged")
    26.         {
    27.             yield return new WaitForSeconds(0.5f);
    28.             DoDamage();
    29.         }
    30.         CSM.PerformList.RemoveAt(0);
    31.         CSM.combatStates = CombatStateMachine.PerformAction.Wait;
    32.         actionStarted = false;
    33.         cur_bar = 0f;
    34.         currentState = TurnState.Processing;
    35.     }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Always point out what line the error is on.

    My quick guess is that you are trying to access list element #0 of an empty list. If that's the problem, either first check whether the list is empty (and do something different if it is), OR if you need for there to be something in the list, then fix however you are generating the list so that the list is never empty.
     
    MikawasakiDigital likes this.
  3. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    The size is 2, although the elements are listed as 0-1.

    For some reason when randomly between the two moves the error randomly appears.

    The code runs fine but randomly crashes.

    Also the error should be on line 9 and 25.
     
  4. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    I think it's calling for a number "Two" when in reality it's 0-1. Is there any way to keep it within that range? Maybe subtracting the number it's calling by 1?
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    What do you mean "should be"? Do you have an actual error or not?

    Lines 9 and 25 are both using a literal zero for the index, so there's no possible way they are accessing index #2. The only direct way those exact lines would throw an index-out-of-bounds error is if your list is empty at the time.
     
    Joe-Censored likes this.
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    1) Figure out the specific line of the error
    2) Add debugging before that line to output things like the length of the collection, what index you're trying to use, etc

    After that the solution should be more or less obvious. If the error isn't telling you the exact line, just add debugging all over the entire method.