Home > OS >  How do I resolve this Collection Modified Error?
How do I resolve this Collection Modified Error?

Time:05-17

To preface, I believe I understand how list modification errors occur. You can't change a list while you're looping them, and in some cases, variables that are linked to the list (I think that's what's happening). I've also searched around quite a bit, but can't seem to find a situation similar to mine.

I'm using TMP UGUI for now as placeholder for a slot machine game I'm working on. I've removed a lot and simplified the code to better understand where the error is occurring. I made a separate list "nextSlotsToCheck" which the loop list will change to before the foreach statement occurs. I believe the list I am looping is not being edited while it is looping, but apparently I'm wrong. Please help.

private void checkSlots(List<GameObject> columns)
{
    // Get all slots in first column
    TextMeshProUGUI[] firstColumnSlots = columns[0].GetComponentsInChildren<TextMeshProUGUI>();

    // Check all slots in first column
    foreach (TextMeshProUGUI firstColumnSlot in firstColumnSlots)
    {
        
        List<TextMeshProUGUI> nextSlotsToCheck = new List<TextMeshProUGUI>();

        // Check following columns
        for (int i = 1; i <= columns.Count - 1; i  )
        {
            List<TextMeshProUGUI> slotsToCheck = new List<TextMeshProUGUI>();

            // Setup for first column check
            if (i == 1)
            {
                slotsToCheck.Add(firstColumnSlot);
            }
            else
            {
                slotsToCheck = nextSlotsToCheck;
            }

            // Check all previously matching slots
            foreach (TextMeshProUGUI currentSlot in slotsToCheck)
            {

                // Check all slots in next column
                foreach (TextMeshProUGUI nextSlot in columns[i].GetComponentsInChildren<TextMeshProUGUI>())
                {
                    nextSlotsToCheck.Add(nextSlot);
                }
            }
        }
    }
}

CodePudding user response:

When your columns-loop goes into that else-case because i != 1, you set:

slotsToCheck = nextSlotsToCheck;

So those two variables point to exactly the same list. Then you are iterating through slotsToCheck and inside it, modify nextSlotsToCheck. So you modify the same list while looping through it, hence the error.

You probably want to swap your slotsToCheck variable to the nextSlots list only after you have looped through it.

EDIT: Tip for the future: Please write in what line an error occurs when you ask. That makes things quicker.

CodePudding user response:

The problem was when I set

slotsToCheck = nextSlotsToCheck;

I didn't realize that this effectively links the lists so changing the second list in

nextSlotsToCheck.Add(nextSlot);

also changes the slotsToCheck list in

foreach (TextMeshProUGUI currentSlot in slotsToCheck)

My solution was to instead of linking the lists, loop through the second one and add each item individually to the first with

foreach (TextMeshProUGUI nextSlot in nextSlotsToCheck)
{
    slotsToCheck.Add(nextSlot);
}

I appreciate the help. Good luck!

  • Related