-1

I have this script:

 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;
            }
        }
    }

this simplest code working fine but showing error

InvalidOperationException: Collection was modified; enumeration operation may not execute. System.Collections.Generic.List1+Enumerator[AnimationControlSpeed].VerifyState () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:778) System.Collections.Generic.List1+Enumerator[AnimationControlSpeed].MoveNext () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:784) CarCollisionDetectionController+c__Iterator0.MoveNext () (at Assets/TramCarAnimationCollisionController/CarCollisionDetectionController.cs:70)

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • 1
    `Collection was modified;` You are changing collection inside `foreach` statement – Valentin Jun 10 '16 at 10:28
  • 1
    Possible duplicate of [What is the best way to modify a list in a 'foreach' loop?](http://stackoverflow.com/questions/759966/what-is-the-best-way-to-modify-a-list-in-a-foreach-loop) – Valentin Jun 10 '16 at 10:29

1 Answers1

2

This error occurs if you do changes to a collection while you iterate with foreach through this collection

Things like this like shown bellow are not allowed because you change loop information while looping it.

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

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

You can fix stuff like this using a for- loop, but be careful with editing you collection while looping it. This actually has a lot potential to generate mistakes.

Greetings.

Constantin Treiber
  • 420
  • 1
  • 7
  • 18