前言
在Unity中运行场景时报错,报错内容如下
原因是在foreach
中不能修改处理的集合,Foreach体内运用了对Collection的Remove方法(或者Add方法)
比如下面这种
foreach (string item in StudentList)
{
items.Add(item);//不可操作
items.Remove(item);//不可操作
}
解决办法
如果遍历时涉及增删操作,则使用for循环
上面的例子改为:
for (int j=0;j< StudentList.Count;j++)
{
StudentList.Add("");
StudentList.Remove("");
}