0
0
Unityframework~10 mins

Tags and layers in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Tags and layers
Create GameObject
Assign Tag
Assign Layer
Use Tag/Layer in Code
Check Tag/Layer in Scripts
Perform Actions Based on Tag/Layer
This flow shows how you create a GameObject, assign tags and layers, then use them in scripts to control behavior.
Execution Sample
Unity
GameObject enemy = new GameObject("Enemy");
enemy.tag = "Enemy";
enemy.layer = LayerMask.NameToLayer("Enemies");

if(enemy.CompareTag("Enemy")) {
    Debug.Log("This is an enemy!");
}
Creates a GameObject, assigns it a tag and layer, then checks the tag in code to print a message.
Execution Table
StepActionVariable/PropertyValueResult/Output
1Create GameObjectenemy.name"Enemy"GameObject created with name 'Enemy'
2Assign tagenemy.tag"Enemy"Tag set to 'Enemy'
3Assign layerenemy.layerLayerMask.NameToLayer("Enemies")Layer set to 'Enemies' layer index
4Check tag with CompareTagenemy.CompareTag("Enemy")trueCondition true
5Print messageDebug.Log"This is an enemy!"Message printed to console
6EndScript ends
💡 Script ends after printing message because tag check is true
Variable Tracker
Variable/PropertyStartAfter Step 1After Step 2After Step 3After Step 4Final
enemy.namenull"Enemy""Enemy""Enemy""Enemy""Enemy"
enemy.tagnull"Untagged""Enemy""Enemy""Enemy""Enemy"
enemy.layerN/A00Layer index of 'Enemies'Layer index of 'Enemies'Layer index of 'Enemies'
CompareTag resultN/AN/AN/AN/Atruetrue
Key Moments - 2 Insights
Why do we use CompareTag() instead of directly checking enemy.tag == "Enemy"?
CompareTag() is optimized by Unity and avoids errors if the tag does not exist. See execution_table row 4 where CompareTag returns true safely.
What happens if the layer name "Enemies" does not exist?
LayerMask.NameToLayer returns -1 if the layer name is missing, so enemy.layer would be set to -1, which means no valid layer. See execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What does enemy.layer get set to?
A-1 because layers are not assigned
BThe integer index of the 'Enemies' layer
CThe string 'Enemies'
DThe tag 'Enemy'
💡 Hint
Check the 'Value' column at step 3 in execution_table
At which step does the script confirm the GameObject has the 'Enemy' tag?
AStep 4
BStep 2
CStep 5
DStep 3
💡 Hint
Look for the CompareTag check in execution_table
If enemy.tag was set to "Player" instead of "Enemy", what would CompareTag("Enemy") return at step 4?
Atrue
Bnull
Cfalse
Dthrows error
💡 Hint
CompareTag returns true only if tags match exactly, see variable_tracker CompareTag result
Concept Snapshot
Tags and Layers in Unity:
- Tags identify GameObjects by name for easy grouping.
- Layers help with rendering and physics filtering.
- Use CompareTag() to check tags safely.
- Assign layers by name with LayerMask.NameToLayer.
- Tags and layers control behavior and interactions.
Full Transcript
This visual execution shows how to create a GameObject in Unity, assign it a tag and a layer, then check the tag in code to perform an action. First, the GameObject named 'Enemy' is created. Then, its tag is set to 'Enemy'. Next, the layer is assigned using LayerMask.NameToLayer with the name 'Enemies'. The script then uses CompareTag to check if the GameObject has the 'Enemy' tag. Since it does, the script prints 'This is an enemy!' to the console. The variable tracker shows how the name, tag, and layer properties change step by step. Key moments clarify why CompareTag is preferred and what happens if a layer name is missing. The quiz tests understanding of layer assignment, tag checking, and CompareTag behavior. This helps beginners see how tags and layers work in Unity scripts.