I have a code, there is a part of it:
public List<Rigidbody2D> caughtRigidbodies = new List<Rigidbody2D>();
void OnTriggerEnter2D(Collider2D other)
{
if (other.GetComponent<Rigidbody2D>() && other.gameObject.CompareTag("mag"))
{
Rigidbody2D r = other.GetComponent<Rigidbody2D>();
if (caughtRigidbodies.Count < 20)
{
if (!caughtRigidbodies.Contains(r))
{
caughtRigidbodies.Add(r);
}
}
}
}
With some reason this code add new elements in list even if the list has more then 20 objects.
How can I fix it?
Sry by my english, it’s not my native language.
edit: at first i have wrote it like
if (!caughtRigidbodies.Contains(r) && caughtRigidbodies.Count < 20)
{
}
But this didn’t work(