0
0
Unityframework~20 mins

Collider2D types (box, circle, polygon) in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Collider2D Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Collider2D Type Check

What will be printed by this Unity C# script when attached to a GameObject with a CircleCollider2D?

using UnityEngine;

public class ColliderTypeCheck : MonoBehaviour {
    void Start() {
        Collider2D col = GetComponent();
        if (col is BoxCollider2D) {
            Debug.Log("Box Collider");
        } else if (col is CircleCollider2D) {
            Debug.Log("Circle Collider");
        } else if (col is PolygonCollider2D) {
            Debug.Log("Polygon Collider");
        } else {
            Debug.Log("Unknown Collider");
        }
    }
}
Unity
using UnityEngine;

public class ColliderTypeCheck : MonoBehaviour {
    void Start() {
        Collider2D col = GetComponent<Collider2D>();
        if (col is BoxCollider2D) {
            Debug.Log("Box Collider");
        } else if (col is CircleCollider2D) {
            Debug.Log("Circle Collider");
        } else if (col is PolygonCollider2D) {
            Debug.Log("Polygon Collider");
        } else {
            Debug.Log("Unknown Collider");
        }
    }
}
AUnknown Collider
BBox Collider
CCircle Collider
DPolygon Collider
Attempts:
2 left
💡 Hint

Think about which collider type is attached to the GameObject.

Predict Output
intermediate
2:00remaining
PolygonCollider2D Points Count

What is the output of this code snippet when the PolygonCollider2D has 3 points defined?

using UnityEngine;

public class PolygonPointsCount : MonoBehaviour {
    void Start() {
        PolygonCollider2D poly = GetComponent();
        Debug.Log(poly.GetPath(0).Length);
    }
}
Unity
using UnityEngine;

public class PolygonPointsCount : MonoBehaviour {
    void Start() {
        PolygonCollider2D poly = GetComponent<PolygonCollider2D>();
        Debug.Log(poly.GetPath(0).Length);
    }
}
A3
B0
C1
DThrows NullReferenceException
Attempts:
2 left
💡 Hint

GetPath(0) returns the first path's points array.

🔧 Debug
advanced
2:30remaining
Why does this BoxCollider2D size not update?

Consider this code snippet attached to a GameObject. The size does not update as expected. What is the cause?

using UnityEngine;

public class BoxSizeChanger : MonoBehaviour {
    void Start() {
        BoxCollider2D box = GetComponent();
        box.size = new Vector2(5, 5);
    }
}
Unity
using UnityEngine;

public class BoxSizeChanger : MonoBehaviour {
    void Start() {
        BoxCollider2D box = GetComponent<BoxCollider2D>();
        box.size = new Vector2(5, 5);
    }
}
AThe size property is read-only and cannot be changed at runtime.
BThe BoxCollider2D component is missing from the GameObject.
CThe BoxCollider2D size is overridden by the attached Rigidbody2D component.
DThe code runs in Start(), but another script resets the size later.
Attempts:
2 left
💡 Hint

Check if the component exists before changing its properties.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in CircleCollider2D assignment

Which option contains the correct syntax to assign a radius of 3.5 to a CircleCollider2D component in Unity C#?

AcircleCollider.radius := 3.5f;
BcircleCollider.radius == 3.5f;
CcircleCollider.radius = 3.5;
DcircleCollider.radius = 3.5f;
Attempts:
2 left
💡 Hint

Remember the assignment operator in C#.

🚀 Application
expert
3:00remaining
Calculate total area covered by multiple Collider2D types

You have a GameObject with three Collider2D components: a BoxCollider2D of size (2,3), a CircleCollider2D of radius 1, and a PolygonCollider2D with a triangle path of points (0,0), (1,0), (0,1). Which code snippet correctly calculates the total approximate area covered by all three colliders?

Afloat totalArea = 2 * 3 + Mathf.PI * 1 * 1 + 0.5f * 1 * 1;
Bfloat totalArea = 2 + 3 + Mathf.PI * 1 + 1.5f;
Cfloat totalArea = (2 + 3) * Mathf.PI * 1 * 1 * 0.5f * 1 * 1;
Dfloat totalArea = Mathf.PI * 2 * 3 + 1 + 0.5f;
Attempts:
2 left
💡 Hint

Recall formulas for rectangle, circle, and triangle areas.