For sake of simplicity; imagine a loop of classes car; where each car contain a list of car systems; the second loop goes through each system to remove them from that car. Ideally, at the end, each car will have no systems.
this is the simplified code.
public class ParseCars: MonoBehaviour
{
foreach (cars a_car in cars_list)
{
// retrieve the systems for the current car
List<car_system> systems_list = a_car.ReturnSystemList();
foreach (car_system a_system in systems_list)
{
a_car.RemoveSystem(a_system);
}
Debug.Log("removed all systems for this car);
}
}
when removing the system, all that I do is to call the Remove method of the list, contained in the cars class.
The first removal is successful; then when it is time to remove the next element, instead of executing again the remove call, it goes back on the previous foreach, loading the next car in the list.
Looking at the name of the next system; I still see the previous, like if the loop would not take the next item in the list. I do not see any exception, it is the first time that I see this behavior, and can't really figure out what is happening, since I get no feedback out of the debugger.