Hello awesome UnityForumCommunity,
I need help with a problem that has stumped me for weeks.
My problem is at the end of this but if you need additional info of my program here it is:
I am sincerely sorry for the long question but I hope it is helpful and any suggestions on how to fix this will be GREATLY appreciated


I have 2 lists:

public List<GameObject> PrefOfNum = new List<GameObject>();
public List<Transform> PointOfNum = new List<Transform>();

And I spawn 5 of the first prefabs/items of the PrefOfNum randomly onto the 5 transform points in PointOfNum
like this:

`public void Spawner() {
for (int i = 0; i < PointOfNum.Count; i++) {

        GameObject temp = PrefOfNum*;*

int RandomIndex = Random.Range(i, PointOfNum.Count);
PrefOfNum = PrefOfNum[RandomIndex];
PrefOfNum[RandomIndex] = temp;
GameObject.Instantiate(PrefOfNum, PointOfNum_.position, PointOfNum*.rotation);
}
Debug.Log(“Spawning has been done”);
}`*

Now I make the user click on the spawned prefabs (note this is a counting game for kids and the objects are all represented by the first five numbers spawned) it deletes the prefab/gameobject that has been spawned and then if the user has clicked the numbers in the right order (1 , 2 , 3 ,4 ,5) I make it start a function to delete the five objects.
public int NumNo;
public static int Count;
public int Lim = 5;
public bool CountStarter = true;_

* // Use this for initialization*
* void OnMouseDown () {*
Destroy(gameObject);

if (CountStarter == true)
{
Count++;
CountChecker();
}
* }*

* // Update is called once per frame*
* void CountChecker () {*
GameObject other = GameObject.Find(“AOBDT”);
CreateObj createObj = other.GetComponent();

if (NumNo != Count)
{
CountStarter = false;
Debug.Log(“No longer counting up”);
}
else if (Count == Lim)
{
Count = 0;
createObj.Deleter();
}
}
}
----------
MY PROBLEM
Once I start a function to delete the first five objects from the list, it only works once.
public List PrefOfNum = new List();
public List PointOfNum = new List();
public int Nums = 20;
public int Minuser = 5;
public void Deleter() {
Debug.Log(“Deleter has been officially started”);

PrefOfNum.Reverse();
for (int i = PrefOfNum.Count - 1; i >= Nums - Minuser; i–) {
PrefOfNum.RemoveAt(i);
}
PrefOfNum.Reverse(0, Nums - Minuser);
Spawner();
}
It needs to keep repeating this sequence of deleting 5 then calling the Spawner(). Then removing the next 5 and calling the Spawner() again till there is no objects left in the PrefOfNum(). Also I was previously able to get it to work till the end of this sequence but it would give me a ArguementOutOfRangeError().
Once again I’m sorry for the lengthy question but please help me out. With many thanks in advance.

This is a C# question more than a Unity question, but sure, why not.

You should not delete items from an array you’re iterating from.

If you remove an item during the iteration, the iteration breaks (number of items changed, giving you the Out of Range error). Same for “foreach” loops. Seriously, rule of thumb, never delete items from the list you’re iterating.

How to fix, though? Create a new list, copying the items you want to keep (rather than delete), then replace the original list with the copy.