Hello folks! I am creating a top-down shooter and I just set up shooting but when my bullets hit the enemy it doesn’t destroy it or the enemy.
public class bullet : MonoBehaviour
{
public GameObject enemy;
private void OnCollisionEnter2D(Collision2D collision)
{
Destroy(gameObject);
Destroy(enemy);
}
}
kurizu
2
I think you misinterpreted the OnCollisionEnter2D function. The object that your bullet collides with is returned as the collision parameter in the function, so if you want to remove that object, you will have to do; Destroy(collision.gameObject).
In order to have objects detect collision in the first place, you will also need to have a Rigidbody on 1 of the objects and obviously a 2d collider on both.
Also, you should first do a check if the object your bullet is colliding with is an enemy, so consider putting a tag on your enemies.