Delete duplicates in a list of vectors

Pretty much as the title says. In the inspector i can see many objects with the same position. I have created some spheres, i then add them to a list, destroy all of them, then get rid of the duplicates in the list then instantiate them back into the game. The problem for me is getting rid of duplicates.

What i have tried and done

  • Used while statements and if statement to scan through and delete duplicates (in between the here comments)
  • Used .distinct().tolist() as suggested in other suggestions which did not work as proven by the 2 debug statements as shown
  • Made sure that the values are all rounded to the same degree of accuracy (3 d.p) which worked as shown in the inspector but it did not help me get rid of duplicates

My code is shown below with important parts mentioned above.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class SphereDeleteRepeat : MonoBehaviour {
    List<GameObject> Spheres;
    GameObject[] gameObjects;
    Rect area;

	// Use this for initialization
	void DoAble () {
        gameObjects = GameObject.FindGameObjectsWithTag("Sphere");
        Debug.Log(gameObjects.Length);
        Spheres = new List<GameObject>();
        foreach (GameObject sph in GameObject.FindGameObjectsWithTag("Sphere"))
        {
            Spheres.Add(sph);
        }
        Debug.Log("First " + Spheres.Count); // 125
        Spheres = Spheres.Distinct().ToList();
        Debug.Log("Last " + Spheres.Count); // 125
        int i = 0;
        while (i < gameObjects.Length)
        {
            Destroy(gameObjects*);*

i = i + 1;
}
// HERE
i = 0;
while (i < Spheres.Count)
{
int i2 = 0;
while (i2 < Spheres.Count)
{
if (i2 != i && Spheres*.transform.position == Spheres[i2].transform.position)*
{
Spheres.RemoveAt(i2);
}
i2++;
}
i++;
}
// HERE
Debug.Log(Spheres.Count);
area.xMin = -2.6f;
area.xMax = 2.6f;
area.yMin = -3;
area.yMax = 3;
i = 0;
while (i < Spheres.Count)
{
if (area.Contains(Spheres*.transform.position) == true)*
{
Instantiate(Spheres_, Spheres*.transform.position, Quaternion.identity);
}
i++;
}
}*_

* // Update is called once per frame*
* void Update () {*
* if (Time.frameCount == 2)*
{
DoAble();
}
* }*
}
I get the error message "ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index" which dissapears when i remove the Spheres.RemoveAt line.
Thanks for any help it is much appreciated as this problem has been annoying me for ages.

Spheres.RemoveAt(i2);
i2–; //add this immediately below Remove()

You need to decrement your index, after you remove the element. Because you will increment again at the end of the loop, if you DON’T decrement, you will skip the item that immediately followed the one you deleted. (is that clear, at all?)

Hmm. not sure if you will need to do the same for the i index, used by the outerloop: but I would think SO.

Spheres.RemoveAt(i2);
i2--; //add this immediately below Remove()
i--;

Note: Be careful, if you choose to optimize this later, and STORE the list size in a variable, rather than use list.Count in your loop’s conditional- you will need to decrement THAT variable too, when you Remove() one.