C# InvalidOperationException:集合已修改;列举
我有这个剧本:C# InvalidOperationException:集合已修改;列举,c#,unity3d,mono,C#,Unity3d,Mono,我有这个剧本: void Update() { //if Trigger is occur (like car collided with another car) and tram trigger is not in progress then resume the animation //it means that stopped car should be animate again if (isTriggerOccur &
void Update()
{
//if Trigger is occur (like car collided with another car) and tram trigger is not in progress then resume the animation
//it means that stopped car should be animate again
if (isTriggerOccur && !objTramTriggerDetector.isTramTriggerInProgress)//&& objTramTriggerDetector.oneTimeOnly
{
Debug.Log("1:- " + triggerObjects.Count);
Debug.Log("2:- " + triggerObjects2.Count);
StartCoroutine(PlayAnimationOneByOne());
StartCoroutine(PlayAnimationOneByOne2());
}
}
IEnumerator PlayAnimationOneByOne(){
foreach (var g in triggerObjects)
{
Debug.Log("t1 :" + g.name, g.gameObject);
//Debug.Log("count : " + triggerObjects.Count);
if (triggerObjects.IndexOf(g) == 0)
{
yield return new WaitForSeconds(Random.Range(.2f, .8f));
}
g.Speed = 0.05f;
yield return new WaitForSeconds(Random.Range(.2f, .8f));
}
//triggerObjects.Clear();
}
IEnumerator PlayAnimationOneByOne2()
{
foreach (var g in triggerObjects2)
{
//Debug.Log(g.name, g.gameObject);
//Debug.Log("count : " + triggerObjects2.Count);
if (triggerObjects.IndexOf(g) == 0)
{
yield return new WaitForSeconds(Random.Range(.2f, .8f));
}
g.Speed = 0.05f;
//triggerObjects2.Remove(g);
yield return new WaitForSeconds(Random.Range(.2f, .8f));
}
//triggerObjects2.Clear();
}
static List<AnimationControlSpeed> triggerObjects = new List<AnimationControlSpeed>();
static List<AnimationControlSpeed> triggerObjects2 = new List<AnimationControlSpeed>();
void OnTriggerEnter(Collider c)
{
if (c.tag == "Car")
{
if (c.gameObject.GetComponent<AnimationControlSpeed>().Speed == 0)
{
if (gameObject.name.Contains("VehicleControl001") || gameObject.name.Contains("VehicleControl002"))
{
if (!triggerObjects.Contains(gameObject.GetComponent<AnimationControlSpeed>()))
{
Debug.Log(gameObject.name + " added", gameObject);
triggerObjects.Add(gameObject.GetComponent<AnimationControlSpeed>());
//Debug.Log("t1 count : " + triggerObjects.Count);
}
}
else if (gameObject.name.Contains("VehicleControl"))
{
if (!triggerObjects2.Contains(gameObject.GetComponent<AnimationControlSpeed>()))
{
//Debug.Log(gameObject.name + " added in t2");
triggerObjects2.Add(gameObject.GetComponent<AnimationControlSpeed>());
//Debug.Log("t2 count : " + triggerObjects2.Count);
}
}
//storing last animation speed
lastSpeed = gameObject.GetComponent<AnimationControlSpeed>().Speed;
gameObject.GetComponent<AnimationControlSpeed>().Speed = 0;
isTriggerOccur = true;
}
}
}
void OnTriggerExit(Collider c)
{
if (c.tag == "Car")
{
if (c.gameObject.GetComponent<AnimationControlSpeed>().Speed == 0)
{
isTriggerOccur = false;
}
}
}
这个最简单的代码工作正常,但显示错误
InvalidOperationException:集合已修改;列举
操作可能无法执行。
System.Collections.Generic.List1+枚举器[AnimationControlSpeed].VerifyState
在
/Users/builduser/buildslave/mono运行时和classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:778
System.Collections.Generic.List1+枚举器[AnimationControlSpeed].MoveNext
在
/Users/builduser/buildslave/mono运行时和classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:784
CarCollisionDetectionController+c__迭代器0.MoveNext
在
资产/电车碰撞控制器/车辆碰撞检测控制器。cs:70
如果在使用foreach迭代此集合时对集合进行更改,则会发生此错误 下面所示的事情是不允许的,因为您在循环时更改了循环信息
List<string> newList = new List<string>();
newList.Add("1");
newList.Add("2");
foreach (string content in newList)
{
newList.Add("3");
}
您可以使用for-loop修复类似的内容,但在循环集合时请小心编辑集合。这实际上很有可能产生错误
问候。收藏已修改;您正在更改foreach语句中的集合可能是的重复项