- Home /
i solved it
Collection Change During Iteration
So I am making an RPG. In order to determine whether enemies/items will be present on the map I have decided to check an XML file after the scene has loaded and destroy any objects that are not supposed to be there. It almost works fantastic. A New or Saved game has a Mapfile of all the locations in the world map that you are currently on. When you load a game the Mapfile is broken up into LocationFile's, and when you load a scene of a location, that LocationFile is broken up into SceneObjectFiles for each of the scenes associated with that location. When the scene is fully loaded, the game checks the SceneObjectFiles to strip the not supposed to be present GameObjects from the scene.
And here is the crash... When the SceneObjects OnEnable() or OnDisable(), they fire an event to tell their manager they have enabled/disabled, and they are added/removed from a list. I am attempting to use that list to find the GameObjects to Destroy, but I get this error (even tho I have tried WaitForSeconds()):
"InvalidOperationException: Collection was modified; enumeration operation may not execute."
IEnumerator setObjects(SceneObjectsFile data)
{
foreach (var sobj in data.sceneObjects)
{
foreach (var mobj in ActionDirector.MapEnts)
{
if (mobj.name == sobj.name && sobj.state != "Alive")
{
GameObject.Destroy(mobj.gameObject);
}
}
}
yield return null;
}