- Home /
Question by
2dfruity · Sep 02, 2014 at 04:28 AM ·
listontriggerenter2d
removing duplicates in list
I have a list of enemies compiled when they enter a trigger, but when they enter the trigger again it adds the same enemy into the list a second time. there must be a way to keep this from happening.
void OnTriggerEnter2D (Collider2D tripwire){
if (tripwire.tag == "Enemy") {
enemyList.Add (tripwire.gameObject);
}
}
Comment
Answer by Eric5h5 · Sep 02, 2014 at 05:06 AM
if (tripwire.CompareTag ("Enemy") && !enemyList.Contains (tripwire.gameObject)) {
enemyList.Add (tripwire.gameObject);
}
Answer by kacyesp · Sep 02, 2014 at 06:05 AM
Use a hash table, not a list. Otherwise the Add and Contains method will take O(n) time versus taking O(1) time.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
LINQ to OrderBy, Distinct and RemoveAll List of GameObjects 0 Answers
GO duplication vs. custom class instances 2 Answers
OnTriggerEnter2D doesn't apply forces correctly,if at all 0 Answers
Problem with Object Pooling 3 Answers