Bird
Raised Fist0
Unityframework~20 mins

Collider2D types (box, circle, polygon) 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
🎖️
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.

Practice

(1/5)
1. Which Collider2D type is best suited for a rectangular game object in Unity?
easy
A. BoxCollider2D
B. CircleCollider2D
C. PolygonCollider2D
D. EdgeCollider2D

Solution

  1. Step 1: Understand the shape of the object

    A rectangle or square has straight edges and right angles.
  2. Step 2: Match the collider type to the shape

    BoxCollider2D fits rectangles and squares perfectly because it creates a box-shaped collision area.
  3. Final Answer:

    BoxCollider2D -> Option A
  4. Quick Check:

    Rectangle shape = BoxCollider2D [OK]
Hint: Rectangles use BoxCollider2D for perfect fit [OK]
Common Mistakes:
  • Choosing CircleCollider2D for rectangles
  • Using PolygonCollider2D unnecessarily for simple shapes
  • Confusing EdgeCollider2D with BoxCollider2D
2. Which of the following is the correct way to add a CircleCollider2D component to a GameObject in C# script?
easy
A. gameObject.AddComponent<BoxCollider2D>();
B. gameObject.AddComponent<PolygonCollider2D>();
C. gameObject.AddComponent<CircleCollider2D>();
D. gameObject.AddComponent<EdgeCollider2D>();

Solution

  1. Step 1: Identify the correct component type

    CircleCollider2D is the component for circular collision shapes.
  2. Step 2: Use AddComponent with the correct type

    The syntax is gameObject.AddComponent<Type>(); so for circle, use CircleCollider2D.
  3. Final Answer:

    gameObject.AddComponent<CircleCollider2D>(); -> Option C
  4. Quick Check:

    Add CircleCollider2D with AddComponent<> [OK]
Hint: Use AddComponent<CircleCollider2D>() for circle colliders [OK]
Common Mistakes:
  • Using wrong collider type in AddComponent
  • Missing angle brackets <> in AddComponent
  • Confusing BoxCollider2D with CircleCollider2D
3. What will happen if you assign a PolygonCollider2D to a GameObject and set its points to form a triangle shape?
medium
A. The GameObject will have a circular collision area ignoring the points.
B. The GameObject will have a triangular collision area matching the points.
C. The GameObject will throw a runtime error due to invalid points.
D. The GameObject will have a box-shaped collision area by default.

Solution

  1. Step 1: Understand PolygonCollider2D behavior

    PolygonCollider2D uses points to define a custom shape for collision.
  2. Step 2: Setting points to triangle shape

    When points form a triangle, the collider matches that triangle shape exactly.
  3. Final Answer:

    Triangular collision area matching points -> Option B
  4. Quick Check:

    PolygonCollider2D shape = points defined [OK]
Hint: PolygonCollider2D matches shape from points given [OK]
Common Mistakes:
  • Assuming PolygonCollider2D defaults to box or circle
  • Expecting errors from valid point sets
  • Confusing PolygonCollider2D with BoxCollider2D
4. You wrote this code to add a BoxCollider2D but it doesn't appear on your GameObject:
var collider = gameObject.AddComponent<BoxCollider2D>;
collider.size = new Vector2(2, 3);
What is the error?
medium
A. AddComponent cannot be used in scripts
B. BoxCollider2D does not have a size property
C. Vector2 should be Vector3 for size
D. Missing parentheses after AddComponent<BoxCollider2D>

Solution

  1. Step 1: Check AddComponent syntax

    AddComponent is a method and requires parentheses: AddComponent<Type>()
  2. Step 2: Identify the missing parentheses

    The code uses AddComponent<BoxCollider2D> without () which causes no component to be added.
  3. Final Answer:

    Missing parentheses after AddComponent<BoxCollider2D> -> Option D
  4. Quick Check:

    AddComponent needs () to work [OK]
Hint: Always add () after AddComponent<Type> [OK]
Common Mistakes:
  • Forgetting parentheses after AddComponent
  • Confusing size property with other collider types
  • Using Vector3 instead of Vector2 for 2D colliders
5. You want to create a complex-shaped 2D character with both circular and polygonal parts. Which approach correctly combines colliders for best collision detection?
hard
A. Add multiple Collider2D components: CircleCollider2D for round parts and PolygonCollider2D for complex parts
B. Use only a single PolygonCollider2D for the entire character
C. Use only CircleCollider2D colliders and ignore polygon shapes
D. Add a BoxCollider2D and scale it to cover all parts

Solution

  1. Step 1: Understand collider combination

    Unity allows multiple Collider2D components on one GameObject for complex shapes.
  2. Step 2: Match collider types to shape parts

    Use CircleCollider2D for round parts and PolygonCollider2D for complex shapes to get accurate collisions.
  3. Step 3: Avoid oversimplifying with one collider

    Single collider types may not fit all parts well, causing inaccurate collisions.
  4. Final Answer:

    Add multiple Collider2D components: CircleCollider2D for round parts and PolygonCollider2D for complex parts -> Option A
  5. Quick Check:

    Combine colliders for complex shapes [OK]
Hint: Use multiple colliders for mixed shapes [OK]
Common Mistakes:
  • Trying to use one collider type for all shapes
  • Ignoring collider overlap and physics impact
  • Scaling box collider to fit complex shapes