- Home /
how to check if object already exists in a list
I have a list
List myList
i add a gameobject to my list
myList.Add(x);
i want to see if x gameobject is already in the list.
Answer by Lovrenc · Jul 28, 2013 at 12:20 AM
You have Contains function which tells you weather you have an element in the list or not. But hence, lists are not the best data collections if you dont want/need duplicates. They get slow pretty fast as you increase number of elements in them.
Click the link and read the page and you will know that you should use if( items.Contains( gameObject ) )
@ByDream: what do you mean? you can simple check it before add it into List like:
if(myList.Contains(x)) //if x is in list
//Do nothing or something you want to
else //if x isnt in list
myList.Add(x); //Add x into list
and Lovrenc is right,there are many documents about List, try to read and understand them first. But anyway, hope you can get the answer you want.
I understand this code. But i have a problem: Im created x gameobject 5 times. Then im attached one of the gameobjects. I want check if exists this 5 gameobjects. But its check only attached gameobject. Hope understand. Sorry for bad English..
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Get first gameobject in a list and cycle through on keypress 1 Answer
How to remove objects from a list ? 3 Answers
Ordering a list of GameObjects 3 Answers
Can't add GameObjects to ArrayList 1 Answer