Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Argument out of range exception

Discussion in 'Scripting' started by digisax, May 22, 2017.

  1. digisax

    digisax

    Joined:
    Feb 10, 2015
    Posts:
    2
    I'm running into an issue that I haven't been able to figure out for the last couple weeks
    This is what I'm using to run through a list of groups of enemies, change their state when needed, and remove the lists that are empty. I get an argument out of range exception on the " if(enemyGroupList.Count == 0 || player.CurrState == PlayerScript.State.Win)" line and I haven't quite been able to get it fixed.

    Thanks.

    Code (CSharp):
    1. void Update () {
    2.        if(enemyGroupList.Count < groupNumber)
    3.         {
    4.             enemyGroupList.Add(createGroup());
    5.         }
    6.  
    7.         // for each list in the group list
    8.         for(int i = enemyGroupList.Count - 1; i >= 0; i--)
    9.         {
    10.             // for each opject in a given list
    11.             for (int j = enemyGroupList[i].Count - 1; i >= 0; i--)
    12.             {
    13.                 // pause and unpause enemies when the player pauses the game or is talking
    14.                 if (player.CurrState == PlayerScript.State.Paused || player.CurrState == PlayerScript.State.Talking)
    15.                 {
    16.                     enemyGroupList[i][j].enabled = false;
    17.                 }
    18.                 else
    19.                 {
    20.                     enemyGroupList[i][j].enabled = true;
    21.                 }
    22.              
    23.                 // remove dead enemies
    24.                 if (enemyGroupList[i][j].Dead)
    25.                 {
    26.                     enemyGroupList[i].RemoveAt(j);
    27.                     // break avoids out of range exceptions
    28.                     break;
    29.                 }
    30.             }
    31.  
    32.             if(enemyGroupList[i].Count == 0 || player.CurrState == PlayerScript.State.Win)
    33.             {
    34.                 enemyGroupList.Remove(enemyGroupList[i]);
    35.                 break;
    36.             }
    37.         }
    38.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Code (CSharp):
    1. for (int j = enemyGroupList.Count - 1; i >= 0; i--)

    You are declaring j here, but then you are checking i
    I think you want to check j in your second for loop
     
  3. digisax

    digisax

    Joined:
    Feb 10, 2015
    Posts:
    2
    Yeah, that has to be it. I must have forgot to change it to j when I copy pasted it. Thanks.