unity dictionary foreach remove
One of the restrictions of a foreach loop is that you can't change the underlying thing you're iterating over.
If you change a dictionary while you're iterating through it, the program has no idea if it's getting out of sync and could end up for instance in an infinite loop.
Instead, you can get a list of the keys in the dictionary and iterate over that list.
Since you won't be changing that list, just using it to change the dictionary, there's no chance of getting out of sync.