Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug OnCollisionEnter not working in my 2D project

Discussion in 'Scripting' started by corruptRadiance, Oct 10, 2021.

  1. corruptRadiance

    corruptRadiance

    Joined:
    Sep 17, 2021
    Posts:
    6
    Code (CSharp):
    1. void OnCollisionEnter(Collision collision)
    2.     {
    3.         Debug.Log("Player has collided with an object");
    4.         Die();
    5.     }
    6.  
    7. void Die()
    8.     {
    9.         SceneManager.LoadScene("Main");
    10.     }


    There's a snippet of the code, and a video of the problem. I have a player with a rigidbody and a box collider, and objects for the player to collide with (which also have rigidbodies and box colliders). For some reason when the player collides with the objects, OnCollisionEnter is not called. I thought this would be a simple issue to solve but for the life of me I can't find any solution.

    Any help is appreciated :)
     
    Last edited: Oct 10, 2021
  2. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    check if any of your colliders are triggers. in that case ontriggerenter happens
     
  3. corruptRadiance

    corruptRadiance

    Joined:
    Sep 17, 2021
    Posts:
    6
    When I set the "Is Trigger" to true on both colliders, they simply pass through one another and no collision/trigger is detected
     
  4. Replace this with
    OnCollisionEnter2D
     
  5. corruptRadiance

    corruptRadiance

    Joined:
    Sep 17, 2021
    Posts:
    6
    My god, you are a LIFESAVER!

    Thank you so much <3