These functions help your game know when two objects touch or overlap. This lets you make things happen, like scoring points or playing sounds.
OnCollisionEnter2D and OnTriggerEnter2D in Unity
Start learning this pattern below
Jump into concepts and practice - no test required
using UnityEngine; public class CollisionExample : MonoBehaviour { // Called when this object collides with another with a Rigidbody2D and Collider2D private void OnCollisionEnter2D(Collision2D collision) { // Your code here } // Called when this object's trigger collider overlaps another collider private void OnTriggerEnter2D(Collider2D other) { // Your code here } }
OnCollisionEnter2D is called when two solid objects with Rigidbody2D and Collider2D touch and collide.
OnTriggerEnter2D is called when an object with a Collider2D set as a trigger overlaps another collider.
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("Collided with " + collision.gameObject.name);
}private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Entered trigger of " + other.gameObject.name);
}// Edge case: What if the object has no Rigidbody2D? // Neither method will be called because physics needs Rigidbody2D to detect collisions or triggers.
// Edge case: What if the collider is not set as trigger? // OnTriggerEnter2D won't run; instead OnCollisionEnter2D will run if colliders are solid.
This script prints messages when the object collides with another solid object or enters a trigger zone. Attach it to a GameObject with Rigidbody2D and Collider2D components. Make sure some colliders are triggers and some are not to see both messages.
using UnityEngine; public class CollisionTriggerDemo : MonoBehaviour { private void OnCollisionEnter2D(Collision2D collision) { Debug.Log($"Collision detected with {collision.gameObject.name}"); } private void OnTriggerEnter2D(Collider2D other) { Debug.Log($"Trigger entered by {other.gameObject.name}"); } }
Time complexity: These methods run only when collisions or triggers happen, so they do not slow down your game continuously.
Space complexity: Minimal, only storing collision info temporarily.
Common mistake: Forgetting to add Rigidbody2D to objects, so these methods never get called.
Use OnCollisionEnter2D for solid object hits and OnTriggerEnter2D for zones or pickups that don't block movement.
OnCollisionEnter2D detects solid collisions between objects with Rigidbody2D and Collider2D.
OnTriggerEnter2D detects when an object enters a trigger collider (a collider set as a trigger).
Both let you run code when objects touch or overlap, helping your game respond to player actions and events.
Practice
Solution
Step 1: Understand collision detection methods
OnCollisionEnter2Dis called when two solid objects collide physically, both having Rigidbody2D and Collider2D components without trigger enabled.Step 2: Differentiate triggers from collisions
OnTriggerEnter2Dis called when an object enters a trigger collider, which is a collider set as a trigger, not a solid collision.Final Answer:
OnCollisionEnter2D -> Option BQuick Check:
Physical collision = OnCollisionEnter2D [OK]
- Confusing trigger events with collision events
- Using OnTriggerEnter2D for solid collisions
- Assuming OnCollisionEnter2D works with trigger colliders
Solution
Step 1: Identify correct method name and parameter type
The method for trigger detection in 2D physics isOnTriggerEnter2Dand it takes aCollider2Dparameter.Step 2: Check parameter types and method names
void OnTriggerEnter2D(Collider2D other) matches the exact signature:void OnTriggerEnter2D(Collider2D other). Other options have wrong parameter types or method names.Final Answer:
void OnTriggerEnter2D(Collider2D other) -> Option DQuick Check:
Trigger 2D method = OnTriggerEnter2D(Collider2D) [OK]
- Using 3D Collider instead of Collider2D
- Mixing OnTriggerEnter with OnTriggerEnter2D
- Wrong parameter type in method signature
void OnTriggerEnter2D(Collider2D other) {
Debug.Log("Trigger entered by " + other.name);
}
void OnCollisionEnter2D(Collision2D collision) {
Debug.Log("Collision with " + collision.gameObject.name);
}Solution
Step 1: Understand trigger vs collision behavior
TheOnTriggerEnter2Dmethod is called when an object enters a trigger collider. Since the collider is set as a trigger, this method will run.Step 2: Check collision method call conditions
OnCollisionEnter2Donly runs on solid collisions, not triggers. So it will not be called here.Final Answer:
Logs "Trigger entered by [object name]" when the other object enters the trigger collider. -> Option AQuick Check:
Trigger collider entered = OnTriggerEnter2D logs [OK]
- Expecting OnCollisionEnter2D to run on triggers
- Assuming both methods run simultaneously
- Confusing Collider and Collision parameter types
OnTriggerEnter2D method never runs when expected. What is the likely problem?
void OnTriggerEnter(Collider2D other) {
Debug.Log("Entered trigger");
}Solution
Step 1: Check method name correctness
The method for 2D trigger detection must be named exactlyOnTriggerEnter2D. The code usesOnTriggerEnter, which is for 3D physics.Step 2: Confirm parameter type and method signature
The parameterCollider2Dis correct for 2D triggers, but the method name mismatch prevents Unity from calling it.Final Answer:
The method name should be OnTriggerEnter2D, not OnTriggerEnter. -> Option CQuick Check:
Correct method name = OnTriggerEnter2D [OK]
- Using 3D method names for 2D physics
- Wrong parameter types
- Expecting return values from event methods
Solution
Step 1: Choose collider settings for collectibles
To allow the player to pass through collectibles without blocking, the collectible's Collider2D must be set as a trigger.Step 2: Select correct detection method
UseOnTriggerEnter2Din the player's script to detect when the player enters the collectible's trigger collider.Final Answer:
Set the collectible's Collider2D as a trigger and use OnTriggerEnter2D in the player's script. -> Option AQuick Check:
Trigger collider + OnTriggerEnter2D = collectible detection [OK]
- Using solid colliders causing player to stop
- Using OnCollisionEnter2D with triggers
- Mixing 3D and 2D physics components
