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

Question My OnCollisionEnter() is not working

Discussion in 'Physics' started by Sekudo, Apr 19, 2023.

  1. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    I have a simple script that just simply detects when I collide with something to simply say a debug message. (previously was a function, but need to fix it so I'm using print() for this) I also have a player that moves around in all four directions, has gravity, and can jump. The player has a box collider. and so does my end goal, so they can't fully touch each other, but the colliders do. However, the message is not printed when I touch it, and I double checked that it had the correct tag. Does anyone know why this happens, or how it can be fixed? Here is a bit of my code: (the other parts are unnecessary)


    Code (CSharp):
    1.     private void OnCollisionEnter(Collision collision)
    2.     {
    3.         if (collision.gameObject.CompareTag("End"))
    4.         {
    5.             print("debug");
    6.         }
    7.     }
    Thank you very much for reading.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,364
    Necessary for a OnCollisionEnter():

    * you're working with 3D stuff, not 2D
    * one or both objects has a Rigidbody
    * both objects have colliders somewhere in their hierarchy
    (if there's a Rigidbody, it can be on any child of the Rigidbody too,
    but the OnCollisionEnter goes to the Rigidbody)​
    * both objects are on layers which are collidable in the Project Settings > Physics > Collision Matrix
    * the colliders are not marked with isTrigger
    * neither collider is a CharacterController
     
  3. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    Oh, I noticed the box did not have a rigidbody. (The player did, I just wanted to make sure.) So I made one and checked all the constraints. However, it still does not work. so now I'm wondering if maybe trying to do the same thing with an Is Trigger would work? Would you be able to tell me how? If not don't worry, I can look it up. Just an idea