2d platformer. character does not get destroyed on contact with spike collider
im creating a 2d platformer and i need to destroy my character when it touches the spikes in the scene. i have attempted changing the code in my player controller but that does not seem to be doing anything regardless of where i position it or what i put in it. both the player and the spike have boxcollider2d on and rigidbody2d and they clearly collide with each other but the player does not get destroyed.
This is my first attempt at altering the player controller with a simple destroy function which doesn't work:
void OnCollisionEnter2D(Collision2D col) {
Debug.Log(col.gameObject.name);
if (col.gameObject.name == "Spike")
{
Destroy(col.gameObject);
}
}
This is the second attempt at the destroy function:
void OnCollisionEnter2D(Collision2D other) {
Debug.Log(other.gameObject.tag);
if (other.gameObject.tag == "Spike")
{
Destroy(gameObject);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612065525im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is it when i make walls in unity 2d my sprite stays on the wall ? 0 Answers
How do I restrict the rotation of the Z axis for the "Player" to a certain extent? (2D) 0 Answers
Issues with an event tigger 0 Answers
How to make rect transform work for ui image to make joystick? 0 Answers
Destroy not working... 0 Answers