- Home /
Unable to remove Vector2's inside of a list that have the same value as each other.
This following code doesn't work the way that it should:
void Start()
{
List<Vector2> myList = new List<Vector2>
{
Vector2.up * 10f,
Vector2.up * 10f
};
myList.Distinct().ToList();
foreach (var item in myList)
{
print(item);
}
}
When I do a print out, it still says that there are two Vector2's inside MyList that have the same value as each other. Wasn't myList.Distinct().ToList() suppose to remove duplicates?
Answer by TreyH · Jul 10, 2018 at 09:38 PM
Distinct returns an enumerable with unique elements. You're fine with the compiler as you aren't required to have any variable receive the new enumerable you've created with Distinct, but your list there isn't being changed.
You need to assign that new one back into that variable.
myList = myList.Distinct().ToList();
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to create a generic list from a group of gameObjects with specific tag? 1 Answer
Javascript containing List<> - can not make them work in WebPlayer 1 Answer
Creating a custom List or Collection 1 Answer
Clear a generic list by replacing it with new list? 2 Answers