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);
}
}
}
}
}