Checking if a list has an object - Error

So i have an OverlapSphere detecting objects and if they have a certain tag, they need to be added to a list if the exact object isn’t already in it. When I run it with two gameobjects, I get 3 items on the list ( one repeated) and when I run it with 3, I get 6…
132832-error.png
public void Join()
{
Collider collider = Physics.OverlapSphere(center, radius);

        foreach (var item in collider)
        {
            if (item.gameObject.tag.Contains("Enemy"))
            {
                if (Enemies.Count < 10)
                {
                    if (!Enemies.Contains(item.gameObject))
                    {
                        Enemies.Add(item.gameObject);
                        GetScript(item.gameObject);
                        item.gameObject.SendMessage("BattleInfo", null, SendMessageOptions.RequireReceiver);
                        print(item.gameObject.name + " added");
                        print(item.GetInstanceID());
                        
                    }
                }
            }
        }
    }

@MoRi92
I’ve checked for a way of looking for duplicates within a list of gameobjects, without constantly adding to a list and causing an issue where the same objects are added multiple times.

Original answer is here: How do I scan a list and remove all duplicates - Questions & Answers - Unity Discussions

Basically you need to add the declaration using System.Linq;

then to create a list of game objects with that specific tag, I’d simply do this:

     Collider[] collider = Physics.OverlapSphere(center, radius);
     //Separate the colliders with those that have the specific tag we want
     for (int i = 0; i < collider.Length; i++)
     {
        if (collides*.gameObject.tag.Contains("Enemy"))*

{
Enemies .Add(collider*.gameObject);*
}
}
//Now let’s go through our list and remove all the duplicates
Enemies = Enemies.Distinct().ToList();

That should be it, let me know if I need to change anything!

hmm worth trying above answer, I expected to give the awnser of a list doesn’t like to be null or empty thus causing an error, but thats not your issue at all, hmm do you have that same script in the scene twice? it looks like each object has its own instance but your getting double the objects you would like. is this correct?