Remove all duplicates from a list

Feeling dumb, but I couldn’t find an answer here or the forums.

I have a list of gameobjects and I want to remove all occurrences of a specific gameobject from that list. I’ve played around using RemoveAll() and ForEach loops, but no luck so far.

To illustrate my request:

List<GameObject> testList = new List<GameObject>();
testList.Add (obj1);
testList.Add (obj1);
testList.Add (obj2);
testList.Add (obj3);

What I would like to be able to do, for example, is to remove all occurrences of obj1 from testList. The testList should then only return obj2 and obj3.

You should be able to use RemoveAll() like this:

list.RemoveAll(obj => obj == objToDelete);

It works for me in Unity… here’s my sample console output:

>  List<String> list = new List<String> {"a","b","b","c"};
>  String.Join(",", list.ToArray());
a,b,b,c
>  list.RemoveAll(obj => obj == "b");
2
>  String.Join(",", list.ToArray());
a,c

Edit: updated to the corrected answer from the comments below.