Search Unity

Question ArgumentOutOfRangeException: Index was out of range.

Discussion in 'Scripting' started by xmonster0, Sep 27, 2022.

  1. xmonster0

    xmonster0

    Joined:
    Oct 24, 2020
    Posts:
    15
    hello, I get an index out of range exception ,
    ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    on this line (line 59) in snippet

    PhotonNetwork.InstantiateRoomObject(zombiePrefabs[zombie].prefab.name, allSpawns[tempspawn[spawn]].transform.position, allSpawns[tempspawn[spawn]].transform.rotation, 0, instData);

    any input would be appreciated!

    Code (CSharp):
    1. if (defineSpawn == true)
    2.                             {
    3.                                 //for loop to spawn the amount of zombies.
    4.                                 for (int i = 0; i < toSpawn; i++)
    5.                                 {
    6.                                     if (zombiePrefabs.Length > 0)
    7.                                     {
    8.  
    9.                                        
    10.                                         var tempspawn = new List<int>();
    11.                                         //get random zombies id from avail zombie prefabs.
    12.                                         int zombie = Random.Range(0, zombiePrefabs.Length);
    13.                                         int globalZombie = System.Array.IndexOf(zws.zombiePrefabs, zombiePrefabs[Random.Range(0, zombiePrefabs.Length)]);
    14.                                         int spawn = 0;
    15.  
    16.                                         //if spawnIndex not set, spawn like the old version
    17.                                         if (zws.zombiePrefabs[globalZombie].spawnIndex.Length > 0)
    18.                                         {
    19.  
    20.                                             //put all possible spawner for given zombie into a list.
    21.                                             for (int x = 0; x < allSpawns.Length; x++)
    22.                                             {
    23.  
    24.                                                 if (!allSpawns[x].lockedToArea || allSpawns[x].lockedToArea.isUnlocked)
    25.                                                 {
    26.                                                     if (zws.zombiePrefabs[globalZombie].spawnIndex.Contains(x))
    27.                                                     {
    28.                                                         tempspawn.Add(x);
    29.                                                     }
    30.                                                 }
    31.                                             }
    32.                                             //pick a random spawn to spawn that zombie.
    33.                                             spawn = Random.Range(0, tempspawn.Count);
    34.  
    35.                                             int skin = Random.Range(0, zombiePrefabs[zombie].skins.Length);
    36.  
    37.                                             object[] instData = new object[2];
    38.  
    39.                                             instData[0] = globalZombie;
    40.                                             instData[1] = skin;
    41.                                             //Debug.Log("zombie name: "+ zombiePrefabs[zombie].skins[skin].name + "spawned location" + allSpawns[tempspawn[spawn]].name);
    42.                                             PhotonNetwork.InstantiateRoomObject(zombiePrefabs[zombie].prefab.name, allSpawns[tempspawn[spawn]].transform.position, allSpawns[tempspawn[spawn]].transform.rotation, 0, instData);
    43.  
    44.                                         }
    45.  
    46.                                         //this is old version code.
    47.                                         else
    48.                                         {
    49.                                             spawn = Random.Range(0, useableSpawns.Length);
    50.                                             int skin = Random.Range(0, zombiePrefabs[zombie].skins.Length);
    51.  
    52.                                             object[] instData = new object[2];
    53.  
    54.                                             instData[0] = globalZombie;
    55.                                             instData[1] = skin;
    56.  
    57.  
    58.                                             //Debug.Log("zombie name: " + zombiePrefabs[zombie].skins[skin].name + "spawned location:" + useableSpawns[spawn].name);
    59.                                             PhotonNetwork.InstantiateRoomObject(zombiePrefabs[zombie].prefab.name, useableSpawns[spawn].transform.position, useableSpawns[spawn].transform.rotation, 0, instData);
    60.  
    61.                                         }
    62.                                     }
    63.                                     else
    64.                                     {
    65.                                         Debug.LogError("No Zombie Prefabs found");
    66.                                     }
    67.                                 }
    68.  
    69.                             }
     
  2. Kurt-Dekker

    Kurt-Dekker

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

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

    Steps to success:
    - find which collection it is (critical first step!)
    - find out why it has fewer items than you expect
    - fix whatever logic is making the indexing value exceed the collection
    - remember you might have more than one instance of this script in your scene/prefab
     
  3. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    Output each array before the issue appears to find which one causes the error.
    Code (CSharp):
    1. Debug.Log(zombiePrefabs[zombie].prefab.name);
    2. Debug.Log(useableSpawns[spawn].transform.position);
    Than it's simply figuring out why it's out of range.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Before you get so bold as to do a:

    You really should consider doing a:

    Code (csharp):
    1. Debug.Log(zombie);
    2. Debug.Log(zombiePrefabs.Length);
    Otherwise you're just recreating the same error on another line, which won't greatly increase knowledge about the data involved.
     
    mopthrow likes this.
  5. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    @xmonster0 The code seems to be good.
    Theoretically the issue can happen if "tempspawn" is empty. Then it will try to reference inexistent "0" index.

    P.S.:
    Just ignore Kurt-Dekker.
    He's been running around emotionally posting pointless comments for some time now. :)
     
    xmonster0 likes this.
  6. xmonster0

    xmonster0

    Joined:
    Oct 24, 2020
    Posts:
    15
    its been figured out , thanks ntu4ka and kurt-dekker
     
    Kurt-Dekker likes this.