- Home /
How to determine if your List has more than 1 of the same object listed?
Well heres what I mean. I want to limit the same number of objects with the same name in my list.
So like I made list string to test it out that has 2 of the following names named [BigRoot].
So what I want to do is.
if(MonsterList.Contains(BigRoot + AnotherBigRoot)){ Then Duplicate a clone GameObject of BigRoot. }
But I can't seem to find out how to do this.
Answer by basil4j · Nov 15, 2015 at 06:35 AM
If you simply dont want duplicates, you could use List(T).Exists to check if its there before adding it. If it exists, don't add another one (or clone it, or w/e).
Or if you want a limited number of the item, count the results of a List(T).FindAll and if greater than 'n', do w/e.
Umm. Hmm This feels like the right way of doing this but... For some reason it's giving me an error.
List<string> results = NewGuy.FindAll("New");
This FindAll and Predicate thing is totally new to me, so i'm having trouble.
The Variable NewGuy is an Array aswell.
On my phoneand baby is complaining so not completely sure this is perfect, but you would do something like.
var results = mylist.FindAll(s => s.Equals("searchstring"));
EDIT: Or List<string> results =
... I guess would work too :)
Sorry about the late reply. I appreciate the time you're giving to help btw.
But I tried it out and I didn't get an error so hopefully I can find a way to put it in with my script.
But I have just 1 other question. What is this "s" for? it seems so random lol.
haha the S can be anything :) I like to think of it as a temp variable which contains the list entry at the current search point, which is then compare to whatever.
There's probably a better description for it, but thats how I remember it.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Delete first object of the same type in a list? C# 1 Answer
List.Contains Method wont work. 1 Answer
Comparing two lists to find the difference 1 Answer
Voting System Using List (C#) 1 Answer