it’s my code
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "fruit") {
GameObject obj = other.gameObject;
fruits.Add (obj);
}
}
so when you throw fruits zombie can pick up them.the problem is that if I make this object child of zombie it will add lots of objects to my list that named fruits.
please watch this movie and you see whats the problem.
https://streamable.com/z7f4v
It seems like it’s just entering collision more than once with an object within the zombie gameObject (might be moving some colliders with the animation?). One solution would be to set an int or a bool to restrict the collision to only one time, and the set back to origin OnTriggerExit2D()
. Something like this:
private bool eatIsEnabled = true;
void OnTriggerEnter2D(Collider2D other)
{
if (eatIsEnabled == true)
{
if (other.tag == "fruit")
{
GameObject obj = other.gameObject;
fruits.Add(obj);
}
eatIsEnabled = false;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (eatIsEnabled == false)
eatIsEnabled = true;
}
Hope this helps 