unity3d Creating and Applying Tags


Example

Tags are typically applied via the editor; however, you can also apply tags via script. Any custom tag must be created via the Tags & Layers window before being applied to a game object.


Setting Tags in the Editor

With one or more game objects selected, you can select a tag from the inspector. Game objects will always carry a single tag; by default, game objects will be tagged as "Untagged". You can also move to the Tags & Layers window, by selecting "Add Tag..."; however, it is important to note that this only takes you to the Tags & Layers window. Any tag you create will not automatically apply to the game object.

The tag drop-down menu can be found directly below the game objects name in the Inspector window.


Setting Tags via Script

You can directly change a game objects tag via code. It is important to note that you must provide a tag from the list of current tags; if you supply a tag that has not already been created, this will result in an error.

As detailed in other examples, using a series of static string variables as opposed to manually writing each tag can ensure consistency and reliability.


The following script demonstrates how we might change a series of game objects tags, using static string references to ensure consistency. Note the assumption that each static string represents a tag that has already been created in the Tags & Layers window.

using UnityEngine;

public class Tagging : MonoBehaviour
{
    static string tagUntagged = "Untagged";
    static string tagPlayer = "Player";
    static string tagEnemy = "Enemy";

    /// <summary>Represents the player character. This game object should 
    /// be linked up via the inspector.</summary>
    public GameObject player;
    /// <summary>Represents all the enemy characters. All enemies should 
    /// be added to the array via the inspector.</summary>
    public GameObject[] enemy;

    void Start ()
    {
        // We ensure that the game object this script is attached to
        // is left untagged by using the default "Untagged" tag.
        gameObject.tag = tagUntagged;

        // We ensure the player has the player tag.
        player.tag = tagUntagged;

        // We loop through the enemy array to ensure they are all tagged.
        for(int i = 0; i < enemy.Length; i++)
        {
            enemy[i].tag = tagEnemy;
        }
    }
}

Creating Custom Tags

Regardless of whether you set tags via the Inspector, or via script, tags must be declared via the Tags & Layers window before use. You can access this window by selecting "Add Tags..." from a game objects tag drop down menu. Alternatively, you can find the window under Edit > Project Settings > Tags and Layers.

Tags can contain spaces and punctuation.

Simply select the + button, enter the desired name and select Save to create a tag. Selecting the - button will remove the currently highlighted tag. Note that in this manner, the tag will be immediately displayed as "(Removed)", and will be completely removed when the project is next reloaded.

Selecting the gear/cog from the top right of the window will allow you to reset all custom options. This will immediately remove all custom tags, along with any custom layer you may have under "Sorting Layers" and "Layers".