Check if a object is in a List

Hello ,

i would check if a object is in a List , i would check if a object have the same characteristic .

I tried Contains but it doesn’t work .

public List<Coordinates> availablePosition;
public void Check(Case c)
{
if(availablePosition.Contains(c.coordinates))
}

what can i do ?

Thanks you .

Other than the obvious syntax error of your posted snippet, what “doesn’t work” about that? If it doesn’t return true, then the object isn’t in the list.

What is Coordinates? The default equality compared used by contains will behave differently for structure and classes. If Coordinates is a class or other reference type it will return true only if the references are equal. So you can have two classes with the same values that are considered unequal. Struts and other value types are equal if their values are all equal.

1 Like

I understand now , my object does not have the same reference , coordinates need to be a Struct .
Thanks you .

1 Like

All good. I recently dealt with a similar problem over on this thread. Check it out if you want to keep coordinates as a class. It’s a lot of hoops to jump through, but you can make classes compare by value.

http://forum.unity3d.com/threads/overriding-equality-stuff-confusion.357997/

1 Like

Thanks a lot !