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.

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