For loop issues

Having an issue that has me stumped.

Update()
{
   CycleList();
}

void CycleList()
{
   for(int i = list.Count - 1; i >= 0; i- -)
   {
      Destroy(list[i]);
      list.RemoveAt(i);
   }
   list.add(something);
   list.add(something);
   list.add(something);
}

This is causing the list to eventually throw a missing reference error to the list itself, and causing the list in the inspector to constantly flash, as though the inspector is updating after all the list items are being destroyed, even with the remove call directly after. Adding a list.Clear() after does not fix the issue. Even pooling the gameobjects into another list, removing them individually or clearing from the current, and destroying items in the other list later in update does not fix the issue.

Edit 1:

I have switch over to pooling after some more testing. Adding a secondary list with a delay on the destroy function started causing the referenced gameobjects to build up in the hierarchy and not get destroyed. Since this is happening every frame, it would quickly build up and ruin frame rate. After adding a booling check so I could stop adding new items, and maybe let the the destroy list w/ its delay catch up, activating the bool would throw errors referencing destroylist.array['index here'].data missing and an unsupported pptr value error. It seems to be related to a bug that was assumed resolved in 2022.2 (I am using 2022.3.0 for this project). I will leave this up as unanswered as I could not find a confirmation that this was indeed the issue, and if it is not, I am still looking to find out the reasoning behind it for future reference.

The problem is that you are destroying the list while you are iterating over it. This is causing the list index to become invalid, which is why you are getting the missing reference error.

To fix the problem, you can either:

  1. Make a copy of the list before you iterate over it and destroy the copy instead of the original list.
  2. Iterate over the list in reverse order and destroy the items as you go. This will ensure that the list index does not become invalid.

Here is an example of how to fix the problem using the first method:

Update()
{
    // Make a copy of the list.
    List<GameObject> copyOfList = new List<GameObject>(list);

    // Iterate over the copy of the list and destroy the items.
    for (int i = copyOfList.Count - 1; i >= 0; i--)
    {
        Destroy(copyOfList[i]);
    }

    // Add new items to the list.
    list.Add(something);
    list.Add(something);
    list.Add(something);
}

Here is an example of how to fix the problem using the second method:

Update()
{
    // Iterate over the list in reverse order and destroy the items.
    for (int i = list.Count - 1; i >= 0; i--)
    {
        Destroy(list[i]);
        list.RemoveAt(i);
    }

    // Add new items to the list.
    list.Add(something);
    list.Add(something);
    list.Add(something);
}

Which method you choose to use is up to you. The first method is a bit more verbose, but it is also more explicit and easier to understand. The second method is more concise, but it is also a bit more cryptic.