Does anyone understand why I keep getting "ArgumentOutOfRangeException" from my code?
My Goal
My goal is to get a random platform to spawn, but if the max uses is reached then delete it from my list to prevent spawning my platform more times than others. If there is only 1 spawn objects in my list then I would like to re-add the prefabs back before spawning anymore.
Problems I am encountering
The first problem I have is that the list is only updated with up to 6/8 of my prefabs.
The second problem I have is that when trying to re-add the prefabs to the list to allow them to be spawned again I receive the "ArgumentOutOfRangeException"
My Code
public void randomTile() { randomOption = Random.Range(0, SpawnObjects.Count); gameObjectUses[SpawnObjects[randomOption].name]++; if (SpawnObjects.Count == 1) { foreach (GameObject i in Resources.FindObjectsOfTypeAll(typeof(GameObject)).Cast<GameObject>().Where(g => g.tag == "Variants").ToList()) { SpawnObjects.Add(i); gameObjectUses[i.name] = 0; Debug.Log("Adding game object " + i.name + "to list"); } } if (gameObjectUses[SpawnObjects[randomOption].name] == maxUses) { SpawnObjects.Remove(SpawnObjects[randomOption]); Debug.Log("Removing game object " + SpawnObjects[randomOption].name + " from list"); } GameObject temp = Instantiate(SpawnObjects[randomOption], nextSpawnPoint, Quaternion.identity); nextSpawnPoint = temp.transform.GetChild(5).transform.position; } void Start() { var taggedObjects = Resources.FindObjectsOfTypeAll(typeof(GameObject)).Cast<GameObject>().Where(g => g.tag == "Var").ToList(); gameObjectUses.Clear(); SpawnObjects.Clear(); foreach (GameObject i in taggedObjects) { SpawnObjects.Add(i); gameObjectUses.Add(i.name, 0); } for (int i = 0; i < SpawnObjects.Count; i++) { randomTile(); } }
Any help or advice is appreciated.
Your answer
Follow this Question
Related Questions
ArgumentOutOfRangeException: Argument is out of range. -> Index 2 Answers
UNET ArgumentOutOfRangeException 0 Answers
Problem when acessing a list from another script? (ArgumentOutOfRangeException) 0 Answers
Class com.playerio.PlayCodeStorage not found... 0 Answers
Advice practice for scripting 0 Answers