Death on Collision not working

This is my very first coding project, so excuse me if this has an obvious solution, but this code is just outright not working and I can’t figure out why

void OnCollisionEnter(Collision collisionInfo)
    {
        if (collisionInfo.collider.tag == "Obstacle")
        {
            Debug.Log ("dead");
        }
    }

Is the method getting called at all? I would start by putting another Debug.Log statement outside of the if statement and logging what the tag actually is. That will tell you:

  • If any collision is being registered at all
  • What the tag is that you’re colliding with and how that differs from your expectations

This should ALWAYS be the first stop. And with the collider/trigger functions, go to the Scripting API docs page for it and make sure your setup meets all the criteria. If it does not, then all the coding in the world won’t work because Unity has very specific requirements vis-a-vis the function of this callback.

I put a debug outside of the if statement and it didn’t go off. Can you please link to the page you’re referring to that includes the criteria for a collision

This would be the manual page to read! Unity - Manual: Introduction to collision

The two tables at the bottom of the page are of particular interest. After verifying you have the right setup, one other thing to check is to make sure the colliders are in layers that are set to interact in your Physics settings.

Where can I find my physics settings? I am currently using one Rigidbody collider and one Kinematic rigidbody collider. If I understand correctly, those two should interact, right?

It absolutely will not even remotely be possible for you to have any kind of meaningful success in Unity without knowing how to use Google to pull up the two above things.

Seriously.

1 Like

Sorry, I didn’t consider searching until a moment ago. I found my physics settings, and I have all the boxes checked in the layer collision matrix, which I assume is the means that my two objects should interact, although I’m not sure

Thanks to both of you guys for helping, I figured it out, I feel kind of dumb for not realizing that there is a different OnCollisionEnter for 2D objects

1 Like