0
0
Unityframework~20 mins

OnCollisionEnter2D and OnTriggerEnter2D in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.