Not destroying objects fast enough and causing another collision

206 views Asked by At

I have a detection if a bullet hits an enemy that's part of a script that's on an enemy:

void OnTriggerEnter (Collider other)
{
    if (other.tag == "Bullet") {
        Destroy (other); // or Destroy (other, 0);
    }
}

The problem is when a bullet hits an enemy, its health drops to zero (from 100) as if it was hit more than once (I also checked with Debug.log).

So is there a way to make the bullet to destroy faster so it won't trigger again?

Also, should this detection script be on the enemy or on the bullet?

1

There are 1 answers

3
deepankar On BEST ANSWER

Instead of Destroy(other) use Destroy(other.gameObject). other is just a collider reference not the actual gameObject itself.

Also just for good practice instead of comparing tag with == use other.CompareTag(String tagName). Its less memory consuming.