Challenge - 5 Problems
Master of Tags and Layers
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Unity C# code using tags?
Consider this Unity C# script attached to a GameObject. What will be printed in the console when the game starts?
Unity
using UnityEngine; public class TagCheck : MonoBehaviour { void Start() { if (gameObject.CompareTag("Player")) { Debug.Log("This is the player."); } else { Debug.Log("Not the player."); } } }
Attempts:
2 left
💡 Hint
Check the tag assigned to the GameObject in the Unity Editor.
✗ Incorrect
The script checks if the GameObject's tag is exactly "Player". If the GameObject does not have the "Player" tag, it prints "Not the player."
❓ Predict Output
intermediate2:00remaining
What layer mask value is created by this code?
In Unity C#, what integer value does this LayerMask represent?
Unity
int layerMask = (1 << LayerMask.NameToLayer("Enemy")) | (1 << LayerMask.NameToLayer("Obstacle")); Debug.Log(layerMask);
Attempts:
2 left
💡 Hint
LayerMask.NameToLayer returns the layer index; shifting 1 by that index sets the bit.
✗ Incorrect
The code sets bits in an integer for the Enemy and Obstacle layers. The exact number depends on their layer indices but is a sum of powers of two.
🔧 Debug
advanced2:00remaining
Why does this Unity script fail to detect the correct layer?
This script is supposed to detect if the collided object is on the "Enemy" layer. Why does it always print "Not enemy layer"?
Unity
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.layer == LayerMask.GetMask("Enemy")) {
Debug.Log("Enemy layer detected");
} else {
Debug.Log("Not enemy layer");
}
}Attempts:
2 left
💡 Hint
Check what LayerMask.GetMask returns versus gameObject.layer value.
✗ Incorrect
LayerMask.GetMask returns a bitmask with bits set for layers, but gameObject.layer is a single integer index. Comparing them directly always fails.
🧠 Conceptual
advanced2:00remaining
How does Unity use layers to optimize physics collisions?
Which statement best describes how Unity uses layers to control physics collision checks?
Attempts:
2 left
💡 Hint
Think about how Unity can skip unnecessary collision checks.
✗ Incorrect
Unity uses a collision matrix where you can specify which layers should collide with which others, improving performance by skipping checks.
❓ Predict Output
expert3:00remaining
What is the output of this Unity C# code using tags and layers?
Given this code snippet, what will be printed in the console when the game starts?
Unity
using UnityEngine; public class TagLayerTest : MonoBehaviour { void Start() { int enemyLayer = LayerMask.NameToLayer("Enemy"); if (gameObject.layer == enemyLayer && gameObject.CompareTag("Enemy")) { Debug.Log("Enemy object detected."); } else if (gameObject.layer == enemyLayer) { Debug.Log("Enemy layer but wrong tag."); } else if (gameObject.CompareTag("Enemy")) { Debug.Log("Enemy tag but wrong layer."); } else { Debug.Log("Not enemy."); } } }
Attempts:
2 left
💡 Hint
Check the GameObject's tag and layer in the Unity Editor before running.
✗ Incorrect
If the GameObject does not have the "Enemy" tag nor is on the "Enemy" layer, the last else branch runs printing "Not enemy."