For loop only detecting duplicates of the first element in a list of GameObjects
I have a list of GameObjects, and I have a for loop that goes through the list each time to remove duplicate objects that have the same name. The for loop below only seems to be pointing out the duplicates of the first element in the list. Could anyone tell me what I'm doing wrong? Thank You!
if (diskList.Count > 1)
{
for (int disk = 0; disk < diskList.Count; disk++)
{
for (int stepper = 0; disk < diskList.Count; stepper++)
{
if (diskList[stepper].name == diskList[disk].name)
{
if(stepper != disk)
{
Debug.Log("Dup Found: " + diskList[stepper].name + "At position " + stepper);
}
}
}
}
}
Answer by binaryuniverse · Jul 10, 2018 at 01:06 AM
If you always want unique instances of GameObjects, then rather than use a List, use a Hashset. It will keep only unique copies.
Also, in this case, your second loop, the middle says "disk < diskList.Count", but your iterator is stepper, so you probably mean stepper < diskList.Count
Your answer
![](https://koobas.hobune.stream/wayback/20220612170359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Duplicate GUI.Label Instances 1 Answer
start position instantiate for loop. c# 1 Answer
Duplicating a scene in unity 1 Answer