In any game that has some kind of interaction between in-game objects, there are colliders attached to virtually everything. What the players' eyes see are only the textures, meshes and UI elements, and their properties like shadows and animations. What a player does not see are colliders – may that be a 2D or a 3D scene, colliders are almost everywhere in a scene.
Ensure the Colliders are Attached
Each of these colliders is attached to some kind of game object to make collisions happen. Colliders are how we find when a game object runs into another, and that's how most of the physics of collision is simulated. When it seems a collider is not working in your Unity scene, there are few things that you might want to look at. Check if the colliders corresponding to the game objects are attached to their respective game objects. If one or both of the colliders are not attached, that's the first thing you should fix.
Check for missing Rigid Body
For the collision detection between two game objects, one of the game objects should contain the rigid body component.
Attach Right Type of Colliders
There are a lot of different colliders available out of the box in Unity. If you're simultaneously working on several projects, you might have mistakenly attached the wrong type of collider. In a 2D scene, you must attach 2D shape colliders with names like CircleCollider or BoxCollider2D. Similarly, in a 3D game, you must attach 3D colliders such as BoxCollider, SphereCollider, MeshCollider or a CapsuleCollider to your object. This is one of the most common mistakes that beginners make when they start a new 2D game project after following a tutorial for a 3D game and vice versa.
Implement Correct Interfaces
Attaching the right types of colliders is only half the job. To detect collisions you must also implement the correct methods depending upon the type of game you're making. If you are creating a 2D game implement methods that append 2D, such as OnCollisionEnter2D, OnCollisionStay2D, OnCollisionExit2D, etc. to work with collisions.
If you're making a 3D game implement methods without the trailing 2D in the method names, such as OnCollisionEnter, OnCollisionStay, OnCollisionExit, etc.
Perform similar checks for methods that check for Triggers instead of Collisions in your scene.
In some cases, a collision may not be detectable due to the collision matrix. The layer-based collision detection technique is a way to make some objects collide with another game object that is present in another layer. Check collision matrix settings in the Physics Settings window (
Project Settings →
Physics), and ensure correct settings for collision between the respective layers.
If you are trying to check collision using a tag, make sure that you are using the correct tag name in the script. For example, if you have to check the collision between the bullets and the enemy in any combat game, you might have set the tag
enemy for enemy objects in Unity Editor. Make sure your script references the tag correctly, eg.
collider.gameobject.CompareTag("emeny") looks okay at first, but there's a typo in the tag name. Look closely to see the tag names match.
Is the Collider a Trigger?
A collision can not be detected if the
Is Trigger attribute is set to the collider. Open the properties pane of the game object and look inside the Collider component to ensure that option is not selected. If a collider has the
Is Trigger attribute set, instead of the
OnCollision...() callbacks, the
OnTrigger...() callbacks are fired. In such cases, collision detection might seem to be not working.
There might be several other reasons why the collision detection might not be working. You can try adding a new rigid body and a new collider to another game object and see if it works before looking any further. In most cases, it's a human error, so I hope the above tips will be helpful.