1.InvalidOperationException: out of sync
问题描述:
今天运行Unity遇到如下错误:
InvalidOperationException: out of sync
System.Collections.Generic.Dictionary`2+Enumerator[System.String,System.Boolean].VerifyState ()…
这里是我的源代码:
foreach(KeyValuePair<string,bool> keyValue in Content.GetComponent<HandleContent>().UserChoiceDic)
{
if(keyValue.Value == true)
{
Destroy(GameObject.Find(keyValue.Key));
Content.GetComponent<HandleContent>().UserChoiceDic.Remove(keyValue.Key);
}
}
原因如下:
foreach在迭代的时候,不能在迭代中删除正在迭代的集合,the program has no idea if it’s getting out of sync and could end up for instance in an infinite loop.事实上,即使是修改集合中的值也是不允许的。
错法一:
for