Collider is the basic physics in a game. But many a times we find ourself with colliders that are not working. These are a few ways you can debug it. But if you don’t wrap up your head around the basics of collider then it might be difficult. This post is not about Unity colliders, it’s about the way to debug a nonfunctional collider. If you want to know in detail about colliders then read our other post on Unity collider.
Let’s start debugging
- You have attached colliders to your objects.
- A collision requires a Rigidbody. At least one of your colliders should have a Rigidbody component.
- Rigidbody should not be marked as isKinematic.
- Understand the collision matrix below.
Next step: Types of Collider and functions
- If your collider is marked as trigger then you should use OnTriggerEnter()
- If your collider is not a trigger then you should use OnCollisionEnter()
- OnTriggerEnter should be used even if only one of your colliders is a trigger.
- O,T,E,C are capital letters in the OnCollisionEnter and OnTriggerEnter functions.
- Check if you forgot to mention the return type of OnCollisioEnter or OnTriggerEnter.(This should show as an error in Unity console or in visual studio code intelliSense)
- Check the spelling of the collision functions.
- If you are using 2D colliders then the collision functions to use or OnCollisionEnter2D and OnTriggerEnter2D.
- The input variable for OnCollisionEnter is Collision and for OntriggerEnter is Collider. (This should also throw an error on the console window)
If all the above is Ok, then let’s use the final tool to find out where the problem is.
The debug statement
If you have any condition inside your collision function that might be the problem. To check if Unity is detecting collision. Put in a Debug.Log statement inside your collision function and run your game.
If your console window displays the message on collision then it’s your condition that’s not working. If the debug message doesn’t show up then run through the steps above one more time.