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... 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());
}
}
}
}
}
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?
Nope, just once in the scene. It doesn't double the objects, but adds a extra one for each one after the first. With 1 gameobject works, but with 2 it will give 3 and with 3 it returns 6...
thought about this after, whats in your constuctor? and did @ f03n1x solution fix your problem? @$$anonymous$$oRi92
if you dont need a ordered list of enemies, probably a hashset is a better option, since you dont need to worry about duplicates.
probably the "error" is caused because each "enemy" gameObject have attached more than a collider, so OverlapSphere seem to trigger once by collider, not by gameobject
Are you calling any debug log info in a constructor? this will cause a double debug log.
Answer by f03n1x · Feb 10, 2019 at 02:14 AM
@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: https://answers.unity.com/questions/154923/how-do-i-scan-a-list-and-remove-all-duplicates.html
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[i].gameObject.tag.Contains("Enemy"))
{
Enemies .Add(collider[i].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!
item.gameObject
is type GameObject
Enemies
seem to be type List
why compare both? how could give true?
I believe you are trying to say to manually iterate and compare the list... maybe a for loop or similar?
I was confused about the whole List .contains condition check, I've changed my answer to what I think should be a proper solution, from my experience anyways.