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");
}
}
} 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"); } } }
Think about which collider type is attached to the GameObject.
The script checks the type of the Collider2D component. Since the GameObject has a CircleCollider2D, it prints "Circle Collider".
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);
}
} using UnityEngine; public class PolygonPointsCount : MonoBehaviour { void Start() { PolygonCollider2D poly = GetComponent<PolygonCollider2D>(); Debug.Log(poly.GetPath(0).Length); } }
GetPath(0) returns the first path's points array.
The PolygonCollider2D has 3 points in its first path, so GetPath(0).Length returns 3.
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);
}
} using UnityEngine; public class BoxSizeChanger : MonoBehaviour { void Start() { BoxCollider2D box = GetComponent<BoxCollider2D>(); box.size = new Vector2(5, 5); } }
Check if the component exists before changing its properties.
If the BoxCollider2D component is missing, GetComponent returns null, so setting size throws NullReferenceException.
Which option contains the correct syntax to assign a radius of 3.5 to a CircleCollider2D component in Unity C#?
Remember the assignment operator in C#.
Option D uses the correct assignment operator '=' and the float literal '3.5f'. Option D uses '==' which is a comparison, C uses a double literal without 'f', and A uses invalid ':=' operator.
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?
Recall formulas for rectangle, circle, and triangle areas.
The BoxCollider2D area is width * height = 2 * 3 = 6. The CircleCollider2D area is π * r² = π * 1² = π. The triangle area is 0.5 * base * height = 0.5 * 1 * 1 = 0.5. Summing these gives 6 + π + 0.5.