Hi! I’m very new to Unity. So I’ve got the following script attached to a “Red Ball” prefab:
void OnCollisionEnter2D (Collision2D collider)
{
if(collider.gameObject.tag == "Bullet")
{
GameInitialise.playerScore += Mathf.RoundToInt(100 * GameInitialise.playerMultiplier);
GameInitialise.playerMultiplier += 0.01f;
Destroy (this.gameObject);
}
}
The prefab is instantiated once a second and the player attempts to shoot it. The intent is that both the object and the bullet that hit it are destroyed.
The script is a virtual copy/paste across all enemy types in the game and works without problems for the other enemies it’s attached to.
The Red Ball uses a 2D circle collider; the bullets also use 2D circle colliders (for now) and are all tagged “Bullet”.
The issue is that when a bullet collides with the Red Ball, the bullet is invariably destroyed but the Red Ball is only sometimes destroyed. It’s possible to happily sit next to the ball and shoot it repeatedly for several seconds without it dying, then it suddenly disappears.
So to troubleshoot this I tried some console stuff which demonstrated the OnCollisionEnter2D for the Red Ball is frequently not triggering (but the bullet’s OnCollisionEnter2D is always triggering).
I also removed all other scripts from the Red Ball to rule out any interference from the Red Ball’s behaviour. I also tried fiddling with the discrete/continuous collision detection settings and the sleeping mode settings for the bullets. Same problem!
Any thoughts? I’m really stumped.