- Home /
C# List foreach problem
Hello everyone. I'm working on a game for Android where the player needs to touch some objects spawning on random locations before they auto-disappear. The problem I'm having is with deactivating the objects:
IEnumerator DestroyCircles ()
{
foreach (GameObject o in circles) //Where circles is a List<GameObject>
{
yield return new WaitForSeconds (destroyTime);
circles[o].SetActive (false);
}
yield break;
}
I get the following two errors:
1) The best overloaded method match for System.Collections.Generic.List<UnityEngine.GameObject>.this[int]' has some invalid arguments; 2) Argument
#1' cannot convert UnityEngine.GameObject' expression to type
int'.
For the first error, it doesn't work if I convert "o" to an int (obviously).
How should I go about this? Any help is greatly appreciated.
Answer by karljj1 · Feb 03, 2015 at 04:50 PM
Your trying to access circles with an array index but using a GameObject instead of an int as an index.
Looks like you have confused a foreach and a for loop. The variable o will be the next item in the list so just use o.
circles[o].SetActive (false);
should be
o.SetActive (false);
EDIT:
Ok i think this is what you want:
IEnumerator DestroyCircles ()
{
yield return new WaitForSeconds (destroyTime); // Put a pause in before we disable the objects
foreach (GameObject o in circles) // Now disable them all in one go.
{
o.SetActive (false);
}
}
I think he was going round in circles.
I'll get my coat
lol Thanks for the answer, but I get this error now:
InvalidOperationException: Collection was modified; enumeration operation may not execute. System.Collections.Generic.List`1+Enumerator[UnityEngine.GameObject].VerifyState () (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:778) System.Collections.Generic.List`1+Enumerator[UnityEngine.GameObject].$$anonymous$$oveNext () (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:784) CircleClassic+c__Iterator0.$$anonymous$$oveNext () (at Assets/Scripts/CircleClassic.cs:54)
Did you make a change to the list whilst the coroutine was running? Use a for loop, it wont cause these errors.
for( int i = 0; i < circles.Count; ++i )
{
circles[i].SetActive( false );
yield return new WaitForSeconds (destroyTime);
}
Or you could put them all into a second list an iterate through that.
It's true you can't modify the List/Collection/Whatever during a foreach, you should however be able to set properties as long as you're not removing or adding to the collection/list that is being iterated over. SetActive shoould not add or remove elements from the collection. This begs the question of whether you're modifying/destroying anything after the yield statement in the foreach loop? If so, that is the source of your issues.
If you don't want them to disappear one after the other, remove the yield return new WaitForSeconds(destroyTime)
then they will all be destroyed at once.