Bird
Raised Fist0
Unityframework~20 mins

Tags and layers in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Tags and Layers
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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.");
        }
    }
}
ANot the player.
BThis is the player.
CNo output, script does nothing.
DRuntime error: Tag not found.
Attempts:
2 left
💡 Hint
Check the tag assigned to the GameObject in the Unity Editor.
❓ Predict Output
intermediate
2: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);
AAlways 0, because NameToLayer returns -1 if layer not found
BA number with bits set for both Enemy and Obstacle layers, e.g., 18
CA random number depending on frame rate
DA syntax error, code won't compile
Attempts:
2 left
💡 Hint
LayerMask.NameToLayer returns the layer index; shifting 1 by that index sets the bit.
🔧 Debug
advanced
2: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");
    }
}
ALayerMask.GetMask returns a bitmask, not a layer index, so comparison fails
Bcollision.gameObject.layer is always zero, so condition never true
COnCollisionEnter is never called because collider is missing
DThe script has a syntax error in the if statement
Attempts:
2 left
💡 Hint
Check what LayerMask.GetMask returns versus gameObject.layer value.
🧠 Conceptual
advanced
2:00remaining
How does Unity use layers to optimize physics collisions?
Which statement best describes how Unity uses layers to control physics collision checks?
ALayers assign tags to GameObjects for identification
BLayers determine the order in which Update() is called on scripts
CUnity uses a collision matrix where layers can be set to ignore collisions with other layers
DLayers automatically disable rendering for objects not in the camera's view
Attempts:
2 left
💡 Hint
Think about how Unity can skip unnecessary collision checks.
❓ Predict Output
expert
3: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.");
        }
    }
}
AEnemy object detected.
BEnemy layer but wrong tag.
CEnemy tag but wrong layer.
DNot enemy.
Attempts:
2 left
💡 Hint
Check the GameObject's tag and layer in the Unity Editor before running.

Practice

(1/5)
1. What is the primary purpose of Tags in Unity?
easy
A. To identify and find GameObjects by name
B. To control the rendering order of objects
C. To group objects for physics collisions
D. To store numerical values for objects

Solution

  1. Step 1: Understand what Tags do

    Tags are used to label GameObjects with a name for easy identification.
  2. Step 2: Compare with Layers

    Layers group objects mainly for collision and rendering, not identification by name.
  3. Final Answer:

    To identify and find GameObjects by name -> Option A
  4. Quick Check:

    Tags = Identify objects by name [OK]
Hint: Tags name objects; layers group them for collisions [OK]
Common Mistakes:
  • Confusing tags with layers
  • Thinking tags control rendering
  • Assuming tags store numbers
2. Which of the following is the correct way to check if a GameObject has a specific tag in C# script?
easy
A. if (gameObject.CheckTag("Enemy"))
B. if (gameObject.tag == "Enemy")
C. if (gameObject.HasTag("Enemy"))
D. if (gameObject.layer == "Enemy")

Solution

  1. Step 1: Recall Unity tag checking syntax

    Unity uses gameObject.tag == "TagName" to check tags.
  2. Step 2: Identify incorrect options

    layer is an int, not string; HasTag and CheckTag are not Unity methods.
  3. Final Answer:

    if (gameObject.tag == "Enemy") -> Option B
  4. Quick Check:

    Check tag with .tag == "Name" [OK]
Hint: Use gameObject.tag == "TagName" to check tags [OK]
Common Mistakes:
  • Using layer property to check tags
  • Calling non-existent methods like HasTag
  • Comparing layer to string
3. Given this code snippet, what will be printed if the GameObject's layer is set to 8?
void Start() {
    if (gameObject.layer == 8) {
        Debug.Log("Layer is 8");
    } else {
        Debug.Log("Other layer");
    }
}
medium
A. Layer is 8
B. Other layer
C. Compile error
D. No output

Solution

  1. Step 1: Understand the layer property

    The layer property is an integer representing the object's layer number.
  2. Step 2: Check the condition

    If the GameObject's layer is 8, the condition gameObject.layer == 8 is true, so it prints "Layer is 8".
  3. Final Answer:

    Layer is 8 -> Option A
  4. Quick Check:

    Layer 8 matches condition, prints "Layer is 8" [OK]
Hint: Layer is an int; compare with number, not string [OK]
Common Mistakes:
  • Comparing layer to string instead of int
  • Assuming layer is a tag
  • Expecting compile errors from valid code
4. What is wrong with this code snippet?
void Update() {
    if (gameObject.tag = "Player") {
        Debug.Log("Player detected");
    }
}
medium
A. Tags cannot be compared in Update method
B. gameObject.tag is not accessible in Update
C. Debug.Log cannot be used inside if statements
D. Using assignment (=) instead of comparison (==) in if condition

Solution

  1. Step 1: Identify the if condition error

    The code uses = which assigns "Player" to gameObject.tag instead of comparing.
  2. Step 2: Understand correct comparison

    To compare, use == operator, so it should be gameObject.tag == "Player".
  3. Final Answer:

    Using assignment (=) instead of comparison (==) in if condition -> Option D
  4. Quick Check:

    Use == to compare tags, not = [OK]
Hint: Use == for comparison, not = assignment [OK]
Common Mistakes:
  • Using = instead of ==
  • Thinking Debug.Log can't be inside if
  • Believing tag is inaccessible in Update
5. You want to make sure your player GameObject only collides with enemies on a specific layer called "EnemyLayer". Which approach correctly uses layers to achieve this in Unity's physics settings?
hard
A. Assign all objects to Default layer and ignore collisions in script
B. Use tags to identify enemies and check collisions manually in code
C. Assign player to "PlayerLayer" and enemies to "EnemyLayer", then configure Layer Collision Matrix to allow collisions only between these layers
D. Set both player and enemies to the same layer and disable collisions globally

Solution

  1. Step 1: Understand layer collision matrix

    Unity's Layer Collision Matrix lets you control which layers collide with each other.
  2. Step 2: Assign layers and configure collisions

    Assign player and enemies to different layers and enable collision only between those layers in the matrix.
  3. Step 3: Evaluate other options

    Using tags for collision is inefficient; ignoring collisions or disabling globally is not selective.
  4. Final Answer:

    Assign player to "PlayerLayer" and enemies to "EnemyLayer", then configure Layer Collision Matrix to allow collisions only between these layers -> Option C
  5. Quick Check:

    Use layers + collision matrix for selective collisions [OK]
Hint: Use Layer Collision Matrix to control collisions by layers [OK]
Common Mistakes:
  • Using tags instead of layers for collision filtering
  • Assigning all objects to Default layer
  • Disabling collisions globally instead of selectively
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard