Colliders in Unity help detect when objects touch or overlap. Different shapes fit different object types better.
Collider2D types (box, circle, polygon) in Unity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Unity
AddComponent<BoxCollider2D>() AddComponent<CircleCollider2D>() AddComponent<PolygonCollider2D>()
Each collider type is a component you add to a GameObject in Unity.
You can adjust size and shape in the Inspector or by code.
Examples
Unity
gameObject.AddComponent<BoxCollider2D>();
Unity
gameObject.AddComponent<CircleCollider2D>();
Unity
gameObject.AddComponent<PolygonCollider2D>();
Sample Program
This script adds three different 2D colliders to the same GameObject and prints their properties.
Unity
using UnityEngine; public class ColliderExample : MonoBehaviour { void Start() { // Add a BoxCollider2D var box = gameObject.AddComponent<BoxCollider2D>(); box.size = new Vector2(2f, 3f); // Add a CircleCollider2D var circle = gameObject.AddComponent<CircleCollider2D>(); circle.radius = 1.5f; // Add a PolygonCollider2D var polygon = gameObject.AddComponent<PolygonCollider2D>(); polygon.points = new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) }; Debug.Log("Box size: " + box.size); Debug.Log("Circle radius: " + circle.radius); Debug.Log("Polygon points count: " + polygon.points.Length); } }
Important Notes
PolygonCollider2D lets you define any shape by setting points.
BoxCollider2D and CircleCollider2D are simpler and faster for performance.
You can have multiple Box, Circle, or Polygon colliders on a GameObject if needed for compound shapes.
Summary
BoxCollider2D is best for rectangles and squares.
CircleCollider2D fits round objects.
PolygonCollider2D works for complex shapes by defining points.
Practice
1. Which
Collider2D type is best suited for a rectangular game object in Unity?easy
Solution
Step 1: Understand the shape of the object
A rectangle or square has straight edges and right angles.Step 2: Match the collider type to the shape
BoxCollider2D fits rectangles and squares perfectly because it creates a box-shaped collision area.Final Answer:
BoxCollider2D -> Option AQuick 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
Solution
Step 1: Identify the correct component type
CircleCollider2D is the component for circular collision shapes.Step 2: Use AddComponent with the correct type
The syntax is gameObject.AddComponent<Type>(); so for circle, use CircleCollider2D.Final Answer:
gameObject.AddComponent<CircleCollider2D>(); -> Option CQuick 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
Solution
Step 1: Understand PolygonCollider2D behavior
PolygonCollider2D uses points to define a custom shape for collision.Step 2: Setting points to triangle shape
When points form a triangle, the collider matches that triangle shape exactly.Final Answer:
Triangular collision area matching points -> Option BQuick 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
Solution
Step 1: Check AddComponent syntax
AddComponent is a method and requires parentheses: AddComponent<Type>()Step 2: Identify the missing parentheses
The code uses AddComponent<BoxCollider2D> without () which causes no component to be added.Final Answer:
Missing parentheses after AddComponent<BoxCollider2D> -> Option DQuick 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
Solution
Step 1: Understand collider combination
Unity allows multiple Collider2D components on one GameObject for complex shapes.Step 2: Match collider types to shape parts
Use CircleCollider2D for round parts and PolygonCollider2D for complex shapes to get accurate collisions.Step 3: Avoid oversimplifying with one collider
Single collider types may not fit all parts well, causing inaccurate collisions.Final Answer:
Add multiple Collider2D components: CircleCollider2D for round parts and PolygonCollider2D for complex parts -> Option AQuick 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
