0
0
Unityframework~10 mins

OnCollisionEnter2D and OnTriggerEnter2D in Unity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to detect a collision with another object.

Unity
void [1](Collision2D collision) {
    Debug.Log("Collision detected with " + collision.gameObject.name);
}
Drag options to blanks, or click blank then click option'
AUpdate
BOnTriggerEnter2D
CStart
DOnCollisionEnter2D
Attempts:
3 left
💡 Hint
Common Mistakes
Using OnTriggerEnter2D instead of OnCollisionEnter2D for collision detection.
Using Start or Update methods which are unrelated.
2fill in blank
medium

Complete the code to detect when another object enters a trigger collider.

Unity
void [1](Collider2D other) {
    Debug.Log("Trigger entered by " + other.gameObject.name);
}
Drag options to blanks, or click blank then click option'
AOnCollisionExit2D
BOnTriggerExit2D
COnTriggerEnter2D
DOnCollisionEnter2D
Attempts:
3 left
💡 Hint
Common Mistakes
Using OnCollisionEnter2D instead of OnTriggerEnter2D for triggers.
Confusing enter and exit methods.
3fill in blank
hard

Fix the error in the method signature to detect collision correctly.

Unity
private void [1](Collision2D collision) {
    Debug.Log("Hit " + collision.gameObject.name);
}
Drag options to blanks, or click blank then click option'
AOnCollisionEnter2D
BOnTriggerEnter2D
COnTriggerEnter
DOnCollisionEnter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3D collision methods with 2D physics.
Mismatching method name and parameter type.
4fill in blank
hard

Fill both blanks to create a method that logs when an object exits a trigger.

Unity
void [1](Collider2D [2]) {
    Debug.Log("Trigger exited by " + [2].gameObject.name);
}
Drag options to blanks, or click blank then click option'
AOnTriggerExit2D
BOnCollisionExit2D
Cother
Dcollision
Attempts:
3 left
💡 Hint
Common Mistakes
Using OnCollisionExit2D instead of OnTriggerExit2D.
Using 'collision' as parameter name for trigger methods.
5fill in blank
hard

Fill all three blanks to create a method that detects collision and logs the tag of the other object if it is 'Enemy'.

Unity
void [1](Collision2D [2]) {
    if ([3].gameObject.tag == "Enemy") {
        Debug.Log("Collided with Enemy");
    }
}
Drag options to blanks, or click blank then click option'
AOnCollisionEnter2D
Bcollision
Cother
DOnTriggerEnter2D
Attempts:
3 left
💡 Hint
Common Mistakes
Using OnTriggerEnter2D instead of OnCollisionEnter2D for collisions.
Using wrong parameter names or inconsistent names.