- Home /
Collision with the ground kills the player using c#(Unity2D)
Im making a 2d game like flappy bird and I'm trying to make when the player collides with the ground it dies but it's not working here is my code from the script
public class Collider2 : MonoBehaviour {
void OnCollisionEnter2D(Collision2D coll) {
Debug.Log("ThisWorks: " + coll.gameObject.tag);
if (coll.gameObject.tag == "bg ground1")
Destroy(gameObject);
}
}
the script is attached to the player and both the player and Ground have box colliders i don't know why its not working?
$$anonymous$$aybe set the colliders to triggers, and try OnTriggerEnter ins$$anonymous$$d? The function name might be a bit different, as I'm working with 3D.
Firstly is the debug.log printed? Try this Destroy(this.gameObject);
However, david is right , it is better to use triggers in this case. Finally it is bad practice to use spaces in names, you should your ground tag to "bgGround1"
Do you have the first Debug Printed? Are you using BoxColliders or BoxCollider2D?. Try to add BoxCollider2D to both game objects. Aso try to print the tag of every game object that collides, the issue may be with the tag name.
Answer by sed · Feb 18, 2015 at 11:22 PM
Make sure the Box Collider you are using is the BoxCollider2D and the rigidbody is the Rigidbody2D.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183417im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Jumping Top Down 2D Game 0 Answers
How to store tile data for placing tiles 2 Answers
How to increase time scale? 1 Answer
Use 2D Effectors with Raycast Collision Method 0 Answers
Game crashes while trying to implement GooglePlayServices 0 Answers