In my Game i want to make it so that when the Laser hits the Asteroid that the LaserProjectile gets destroyed by the Asteroid. I dont want to attatch the Destroy function in the Laser because there will be enemys and if i atttatch the Destroy function on my laser they will always be oneshot. I placed these lines of Code on my Asteroids but the Laser doesnt get Destroyed. Laser has a BoxCollider2D and a Rigidbody2D. The Asteroids got a PolygonCollider2D and a Rigidbosy2D. void OnCollisionEnter2D(Collision2D col){ if (col.gameObject.tag == "Laser") { Destroy (gameObject); } }
it looks correct to me. Can you though in a Debug.Log("OnCollisionEnter"); to verify that the OnCollisionEnter is being used. Verify that the Colliders is not set to a trigger.
Wait, so you want to destroy the laser and not just the asteroid? So... Destroy(col.gameObject); ? Right now you're destroying the asteroid, but not doing anything with the laser.
I just showed you how. Add that code above the code to destroy the asteroid. Or replace it if you don't want to destroy the asteroid.
Okey i want to destroy the laser but not the asteroid and if the playershit collides with the asteroid it shouldnt get destroyed. EDIT. I forgot to add the Tag to my Laser now it works perfectly fine Sorry if I caused some trouble.
it should be this to destroy the laser Code (CSharp): void OnCollisionEnter2D(Collision2D col) { Debug.Log("Collision with " + col.gameObject.name); if (col.gameObject.tag == "Laser") { Destroy (col.gameObject); } }