2016-06-10 77 views
-1
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:集合已被修改;枚舉

出現InvalidOperationException:集合已修改;枚舉 操作可能無法執行。 System.Collections.Generic.List 1+Enumerator[AnimationControlSpeed].VerifyState () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:778) System.Collections.Generic.List 1 + Enumerator [AnimationControlSpeed] .MoveNext ()(at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections。通用/ List.cs:784) CarCollisionDetectionController + c__Iterator0.MoveNext ()(在 資產/ TramCarAnimationCollisionController/CarCollisionDetectionController.cs:70)

+1

'集合被修改了;'你正在改變'foreach'語句中的集合 – Valentin

+1

[在'foreach'循環中修改列表的最佳方式是什麼?](http://stackoverflow.com/questions/759966 /什麼,是最最好的方式對修改 - 一個列表-IN-A-的foreach循環) – Valentin

回答

1

,如果你同時用迭代做更改的集合時出現此錯誤通過這個系列的foreach

像這樣的事情像笑因爲您在循環時更改循環信息,所以不允許使用波紋管。

List<string> newList = new List<string>(); 
       newList.Add("1"); 
       newList.Add("2"); 

       foreach (string content in newList) 
       { 
         newList.Add("3"); 
       } 

你可以使用for循環修復這樣的東西,但要小心編輯循環它的集合。這實際上有很多潛在的錯誤產生。

問候。

相關問題