Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

InvalidOperationException: Collection was modified; enumeration operation may not execute.

Discussion in 'Scripting' started by T.A.G., Jun 18, 2014.

Thread Status:
Not open for further replies.
  1. T.A.G.

    T.A.G.

    Joined:
    Sep 17, 2013
    Posts:
    38
    Im getting errors on the foreach statement guys. can you help me?. sometimes it freezes unity

    InvalidOperationException: Collection was modified; enumeration operation may not execute.


    Code (csharp):
    1.  
    2. void GetWord(){
    3.  
    4.         matchedIndex = false;
    5.  
    6.         wordIndex = Random.Range(0, words.Length);
    7.         generatedWord = words[wordIndex];
    8.  
    9.         foreach(string myString in usedWords){
    10.             if(generatedWord == myString){
    11.                 print("HAVE MATCHED");
    12.                 matchedIndex = true;
    13.                 GetWord();
    14.             }
    15.         }
    16.  
    17.         if(matchedIndex == false){
    18.             usedWords.Add(generatedWord);
    19.             WordMatching(generatedWord);
    20.         }
    21.     }
    22.  
     
    Pogromer and gamermaker01 like this.
  2. Dabeh

    Dabeh

    Joined:
    Oct 26, 2011
    Posts:
    1,614
    Your collection is being altered as you use it, you're adding to the list inside of a for each statement that is iterating through that list. A quick hack is to make a copy of this list before you start using it since the only other option is to rewrite the whole thing since your structure is flawed.

    Also, wrong subforum.

    Hack:
    Code (csharp):
    1. foreach(string myString in usedWords)
    Code (csharp):
    1. foreach(string myString in usedWords.ToList())
     
    Verne33, ilmario, dick and 12 others like this.
  3. dyox

    dyox

    Joined:
    Aug 19, 2011
    Posts:
    619
    Code (CSharp):
    1. for(int i=0;i<useWords.Lenght;++i)
    2. {
    3.             if(generatedWord == useWords[i])
    4.             {
    5.                 print("HAVE MATCHED");
    6.                 matchedIndex = true;
    7.                 GetWord();
    8.             }
    9. }
     
    Ottobed and Magiichan like this.
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Use a for loop, instead of a foreach loop.
    You can't set a new value to the iterated object.
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    IEnumerable guarantee that we can go through all elements, in order. However, if you remove an item, add an item, or change the order, this guarantee is broken. So it is not allowed.

    Secondly, I know recursive functions are super cool and fun to make! Buuuuutttttt..... they are a good source of bugs. >.>
     
  6. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    yes i got this one one this

    twaypoints = new List<GameObject>(GameObject.FindGameObjectsWithTag("STR"));
    int count = 0;
    GameObject last = gameObject;
    foreach (GameObject item in twaypoints) {

    if (count!= 0)
    last =item;
    GameObject pobj = proche (last);
    twaypoints.Remove(pobj);
    waypoints.Add (pobj);
    count +=1;
    }

    usedWords.ToList()) look not work
     
  7. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    272
    I was using a list and copied it to an array with .ToArray() and that worked :)
     
  8. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    This sounds like a job for ... not recursion. Copying it to a separate list would make it work, technically, but you're still doing a bunch of looping and calls unnecessarily. What about:

    Code (csharp):
    1. void GetWord () {
    2.   for(bool alreadyUsed = true; alreadyUsed;) {
    3.      wordIndex = Random.Range(0,words.Length);
    4.      generatedWord = words[wordIndex];
    5.      alreadyUsed = usedWords.Contains(generatedWord);
    6.   }
    7.  
    8.   usedWords.Add(generatedWord);
    9.   WordMatching(generatedWord);
    10. }
     
  9. irantutors

    irantutors

    Joined:
    May 16, 2018
    Posts:
    2
    foreach (var item in lst.ToArray())
    {

    }
     
  10. tomosbach

    tomosbach

    Joined:
    Feb 25, 2021
    Posts:
    7
    You da best <3
     
    TipPrograms likes this.
  11. Ottobed

    Ottobed

    Joined:
    Jul 6, 2020
    Posts:
    3
    useWords.Length*
     
  12. Miryum

    Miryum

    Joined:
    Jul 25, 2021
    Posts:
    11
    I had a similar problem, I used for() instad of foreach() and the problem solved.
     
  13. TipPrograms

    TipPrograms

    Joined:
    Oct 25, 2022
    Posts:
    3
    I just did .ToArray() for it
     
Thread Status:
Not open for further replies.