Error with Linq ForEach method when trying to build solution

I got a method that’s meant to destroy my objects:

private void ResetMeter()
 {
 lines.Clear();

 var children = new List<GameObject>();

 foreach (Transform child in TicksPanel.transform)
 children.Add(child.gameObject);

 foreach (Transform child in LinesPanel.transform)
 children.Add(child.gameObject);

 //foreach (var child in children)
 // Destroy(child);

children.ForEach(x => Destroy(x));
 }

Now, this works perfectly fine in the unity editor
but if i try to build the solution, i get an error:
‘System.Collections.Generic.List<UnityEngine.GameObject>’ does not contain a definition for ‘ForEach’ and no extension method ‘ForEach’

But if i use the regular non-linq ForEach (which i’ve commented in the code) it works fine and i can build fine.
why can’t i use the linq?

(and yes, i am using “using System.Linq;”)

Try

foreach (GameObject c in children)
Destroy(c);

I have a feeling you didn’t read my question :stuck_out_tongue:
(and also, look at what i commented in code…)

1 Like

What platform are you targetting and what do your build and player settings look like?

I’m targeting Windows Store (windows 8.1)
and i checked developers build, and C# projects

Based on some quick Internet research, I don’t think there is a ForEach method in Linq. There is, however, a ForEach method in the List class, which is the object you’re using, so that’s presumably what you mean.

I did find this discussion saying that it was removed from the “Metro” version of the library, which may be your issue.

It’s an extension method of List but it’s in the System.Linq namespace.

But yes - appears to be a platform issue.

MSDN lists it as a class method, not an extension method.

well, you learn something new every day :slight_smile:
thanks for the find.
didn’t really matter much, but I was just curious.

and i gotta say, the reason for removing it is just weird :S
they said it’s because you could change the list while iterating over it… but you could do the same thing in a normal ForEach loop too…

And Antistone, that’s weird, cause you have to be using linq to get that method.

I’m aware.

You actually can’t modify the collection itself inside a foreach loop.