ArgumentOutOfRangeException: Index was out of range.

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!

if (defineSpawn == true)
                            {
                                //for loop to spawn the amount of zombies.
                                for (int i = 0; i < toSpawn; i++)
                                {
                                    if (zombiePrefabs.Length > 0)
                                    {

                                       
                                        var tempspawn = new List<int>();
                                        //get random zombies id from avail zombie prefabs.
                                        int zombie = Random.Range(0, zombiePrefabs.Length);
                                        int globalZombie = System.Array.IndexOf(zws.zombiePrefabs, zombiePrefabs[Random.Range(0, zombiePrefabs.Length)]);
                                        int spawn = 0;

                                        //if spawnIndex not set, spawn like the old version
                                        if (zws.zombiePrefabs[globalZombie].spawnIndex.Length > 0)
                                        {

                                            //put all possible spawner for given zombie into a list.
                                            for (int x = 0; x < allSpawns.Length; x++)
                                            {

                                                if (!allSpawns[x].lockedToArea || allSpawns[x].lockedToArea.isUnlocked)
                                                {
                                                    if (zws.zombiePrefabs[globalZombie].spawnIndex.Contains(x))
                                                    {
                                                        tempspawn.Add(x);
                                                    }
                                                }
                                            }
                                            //pick a random spawn to spawn that zombie.
                                            spawn = Random.Range(0, tempspawn.Count);

                                            int skin = Random.Range(0, zombiePrefabs[zombie].skins.Length);

                                            object[] instData = new object[2];

                                            instData[0] = globalZombie;
                                            instData[1] = skin;
                                            //Debug.Log("zombie name: "+ zombiePrefabs[zombie].skins[skin].name + "spawned location" + allSpawns[tempspawn[spawn]].name);
                                            PhotonNetwork.InstantiateRoomObject(zombiePrefabs[zombie].prefab.name, allSpawns[tempspawn[spawn]].transform.position, allSpawns[tempspawn[spawn]].transform.rotation, 0, instData);

                                        }

                                        //this is old version code.
                                        else
                                        {
                                            spawn = Random.Range(0, useableSpawns.Length);
                                            int skin = Random.Range(0, zombiePrefabs[zombie].skins.Length);

                                            object[] instData = new object[2];

                                            instData[0] = globalZombie;
                                            instData[1] = skin;


                                            //Debug.Log("zombie name: " + zombiePrefabs[zombie].skins[skin].name + "spawned location:" + useableSpawns[spawn].name);
                                            PhotonNetwork.InstantiateRoomObject(zombiePrefabs[zombie].prefab.name, useableSpawns[spawn].transform.position, useableSpawns[spawn].transform.rotation, 0, instData);

                                        }
                                    }
                                    else
                                    {
                                        Debug.LogError("No Zombie Prefabs found");
                                    }
                                }

                            }

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

Output each array before the issue appears to find which one causes the error.

Debug.Log(zombiePrefabs[zombie].prefab.name);
Debug.Log(useableSpawns[spawn].transform.position);

Than it’s simply figuring out why it’s out of range.

Before you get so bold as to do a:

You really should consider doing a:

Debug.Log(zombie);
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.

1 Like

@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. :slight_smile:

1 Like

its been figured out , thanks ntu4ka and kurt-dekker

1 Like