Collision2D not working
I followed a snake game tutorial and am trying to make it die if touches itself. For that I made a Tag for the new parts of the snake body as "Body" and wrote:
void OnCollisionEnter(Collider2D coll)
{
if (coll.gameObject.tag == "Body")
{
Debug.Log("fj");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
For some reason this does not work, however if I set the collider2d as Trigger and add it to my OnTriggerEnter function, it works like a charm.
Are there any rules for OnCollisionEnter that I am not doing right?
I think the only rule is to have a rigidbody on it. I assume you didn't change the collision layers.
Answer by vintar · Jun 10, 2016 at 04:02 PM
You need to use OnCollisionEnter2D
Still doesn't work :/, I have a rigidbody with kinematic on and gravity 0.
OnCollisionEnter2D(Collision2D coll)...But you set Collider2D as parameter, I think it should be a problem.
OnCollisionEnter2D(Collision2D coll)...But you set Collider2D as parameter, I think it should be a problem.
Answer by adityapatnaik25 · Oct 19, 2019 at 06:24 AM
Uncheck the isTrigger flag on the Collider component, solved the problemfor me(if you are using OnCollisionEnter2D).
- If that didn't work for you try doing these:
Change the Body type to Dynamic and not kinematic if you have a Rigidbody2D.
Check the Zee position of your spawner, source object,target object. Make sure that they are on the same Zee because otherwise they wont collide obviously.
Check that your script and class names match.
Your answer
![](https://koobas.hobune.stream/wayback/20220612082642im_/https://answers.unity.com/themes/thub/images/avi.jpg)