Bird
Raised Fist0
Unityframework~20 mins

OnCollisionEnter2D and OnTriggerEnter2D in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Unity Collision Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when a collision occurs?

Consider this Unity C# script attached to a GameObject with a Rigidbody2D and Collider2D (not set as trigger). What will be printed when this object collides with another object?

Unity
using UnityEngine;

public class CollisionTest : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log($"Collided with {collision.gameObject.name}");
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log($"Triggered by {other.gameObject.name}");
    }
}
ACollided with Enemy
BTriggered by Enemy
CNo output
DBoth 'Collided with Enemy' and 'Triggered by Enemy'
Attempts:
2 left
💡 Hint

Think about the difference between a collider set as trigger and a normal collider.

Predict Output
intermediate
2:00remaining
What happens when a trigger collider is entered?

Given this script on a GameObject with a Collider2D set as trigger and a Rigidbody2D, what will be printed when another GameObject enters its trigger area?

Unity
using UnityEngine;

public class TriggerTest : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("Collision detected");
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log($"Entered trigger by {other.gameObject.name}");
    }
}
AEntered trigger by Player
BNo output
CCollision detected
DBoth 'Collision detected' and 'Entered trigger by Player'
Attempts:
2 left
💡 Hint

Remember which method is called for trigger colliders.

🔧 Debug
advanced
2:00remaining
Why does OnTriggerEnter2D not get called?

A developer wrote this script but OnTriggerEnter2D never runs when expected. What is the most likely reason?

Unity
using UnityEngine;

public class TriggerDebug : MonoBehaviour
{
    void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log("Trigger entered");
    }
}
AThe script is missing Start() method
BThe collider on this GameObject is not set as trigger
CThe Rigidbody2D is missing on this GameObject
DThe method name is incorrect, it should be OnTriggerEnter
Attempts:
2 left
💡 Hint

Check the collider settings on the GameObject.

🧠 Conceptual
advanced
2:00remaining
Which statement about OnCollisionEnter2D and OnTriggerEnter2D is true?

Choose the correct statement about how Unity calls OnCollisionEnter2D and OnTriggerEnter2D methods.

AOnTriggerEnter2D requires both colliders to have Rigidbody2D components
BOnCollisionEnter2D is called only if both colliders are triggers
COnCollisionEnter2D is called when a collider marked as trigger is entered
DOnTriggerEnter2D is called when a collider marked as trigger is entered by another collider with Rigidbody2D
Attempts:
2 left
💡 Hint

Think about the role of triggers and Rigidbody2D in collision and trigger events.

Predict Output
expert
2:00remaining
What is the output of this script when two objects collide and one has a trigger collider?

Analyze the following script attached to a GameObject with a Rigidbody2D and a Collider2D set as trigger. Another GameObject with Rigidbody2D and a normal collider collides with it. What will be printed?

Unity
using UnityEngine;

public class MixedCollision : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("Collision detected");
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log("Trigger detected");
    }
}
ABoth 'Collision detected' and 'Trigger detected'
BCollision detected
CTrigger detected
DNo output
Attempts:
2 left
💡 Hint

Remember how Unity treats collisions between trigger and non-trigger colliders.

Practice

(1/5)
1. Which Unity method is called when two solid objects with Rigidbody2D and Collider2D components collide physically?
easy
A. OnTriggerEnter2D
B. OnCollisionEnter2D
C. OnCollisionExit2D
D. OnTriggerExit2D

Solution

  1. Step 1: Understand collision detection methods

    OnCollisionEnter2D is called when two solid objects collide physically, both having Rigidbody2D and Collider2D components without trigger enabled.
  2. Step 2: Differentiate triggers from collisions

    OnTriggerEnter2D is called when an object enters a trigger collider, which is a collider set as a trigger, not a solid collision.
  3. Final Answer:

    OnCollisionEnter2D -> Option B
  4. Quick Check:

    Physical collision = OnCollisionEnter2D [OK]
Hint: Solid collisions use OnCollisionEnter2D, triggers use OnTriggerEnter2D [OK]
Common Mistakes:
  • Confusing trigger events with collision events
  • Using OnTriggerEnter2D for solid collisions
  • Assuming OnCollisionEnter2D works with trigger colliders
2. Which of the following is the correct method signature for detecting trigger entry in a 2D Unity script?
easy
A. void OnTriggerEnter(Collider other)
B. void OnCollisionEnter2D(Collider2D other)
C. void OnTriggerEnter2D(Collider other)
D. void OnTriggerEnter2D(Collider2D other)

Solution

  1. Step 1: Identify correct method name and parameter type

    The method for trigger detection in 2D physics is OnTriggerEnter2D and it takes a Collider2D parameter.
  2. 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.
  3. Final Answer:

    void OnTriggerEnter2D(Collider2D other) -> Option D
  4. Quick Check:

    Trigger 2D method = OnTriggerEnter2D(Collider2D) [OK]
Hint: Use OnTriggerEnter2D with Collider2D parameter for 2D triggers [OK]
Common Mistakes:
  • Using 3D Collider instead of Collider2D
  • Mixing OnTriggerEnter with OnTriggerEnter2D
  • Wrong parameter type in method signature
3. What will happen when the following Unity script is attached to a GameObject with a Collider2D set as a trigger, and another Rigidbody2D object enters it?
void OnTriggerEnter2D(Collider2D other) {
    Debug.Log("Trigger entered by " + other.name);
}

void OnCollisionEnter2D(Collision2D collision) {
    Debug.Log("Collision with " + collision.gameObject.name);
}
medium
A. Logs "Trigger entered by [object name]" when the other object enters the trigger collider.
B. Logs "Collision with [object name]" when the other object enters the trigger collider.
C. Logs both messages when the other object enters the trigger collider.
D. No logs appear because the methods are incorrect.

Solution

  1. Step 1: Understand trigger vs collision behavior

    The OnTriggerEnter2D method is called when an object enters a trigger collider. Since the collider is set as a trigger, this method will run.
  2. Step 2: Check collision method call conditions

    OnCollisionEnter2D only runs on solid collisions, not triggers. So it will not be called here.
  3. Final Answer:

    Logs "Trigger entered by [object name]" when the other object enters the trigger collider. -> Option A
  4. Quick Check:

    Trigger collider entered = OnTriggerEnter2D logs [OK]
Hint: Trigger colliders call OnTriggerEnter2D, not OnCollisionEnter2D [OK]
Common Mistakes:
  • Expecting OnCollisionEnter2D to run on triggers
  • Assuming both methods run simultaneously
  • Confusing Collider and Collision parameter types
4. A developer wrote this code but the OnTriggerEnter2D method never runs when expected. What is the likely problem?
void OnTriggerEnter(Collider2D other) {
    Debug.Log("Entered trigger");
}
medium
A. The method must return a boolean value.
B. The parameter type should be Collider, not Collider2D.
C. The method name should be OnTriggerEnter2D, not OnTriggerEnter.
D. The method should be private, not public.

Solution

  1. Step 1: Check method name correctness

    The method for 2D trigger detection must be named exactly OnTriggerEnter2D. The code uses OnTriggerEnter, which is for 3D physics.
  2. Step 2: Confirm parameter type and method signature

    The parameter Collider2D is correct for 2D triggers, but the method name mismatch prevents Unity from calling it.
  3. Final Answer:

    The method name should be OnTriggerEnter2D, not OnTriggerEnter. -> Option C
  4. Quick Check:

    Correct method name = OnTriggerEnter2D [OK]
Hint: Use exact method names for 2D events: OnTriggerEnter2D [OK]
Common Mistakes:
  • Using 3D method names for 2D physics
  • Wrong parameter types
  • Expecting return values from event methods
5. You want to detect when a player touches a collectible item in a 2D game without blocking movement. Which setup and method should you use to detect the event correctly?
hard
A. Set the collectible's Collider2D as a trigger and use OnTriggerEnter2D in the player's script.
B. Set the collectible's Collider2D as non-trigger and use OnCollisionEnter2D in the player's script.
C. Set both player and collectible colliders as triggers and use OnCollisionEnter2D.
D. Use OnTriggerEnter with 3D colliders on both objects.

Solution

  1. 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.
  2. Step 2: Select correct detection method

    Use OnTriggerEnter2D in the player's script to detect when the player enters the collectible's trigger collider.
  3. Final Answer:

    Set the collectible's Collider2D as a trigger and use OnTriggerEnter2D in the player's script. -> Option A
  4. Quick Check:

    Trigger collider + OnTriggerEnter2D = collectible detection [OK]
Hint: Use triggers for collectibles to avoid blocking player movement [OK]
Common Mistakes:
  • Using solid colliders causing player to stop
  • Using OnCollisionEnter2D with triggers
  • Mixing 3D and 2D physics components