Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


tag

The tag assigned to the GameObject.
Read time 1 minuteLast updated 4 days ago

Definition

  • Type: Property
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule
public string tag { get; set; }

Remarks

A tag can be used to identify a GameObject. Tags must be declared in the Tags and Layers manager before using them.
Note: Do not set a tag from Awake() or OnValidate(). The order in which
Awake
is called is not deterministic between components and a tag can be overwritten when its
Awake
is called. If you do this, Unity generates the warning
SendMessage cannot be called during Awake, CheckConsistency, or OnValidate
.
The example below sets the current GameObject's tag to "Player" and then implements MonoBehaviour.OnTriggerEnter(Collider) to check if the Collider on the other object involved in a collision with this object is tagged "Enemy".
using UnityEngine;public class Example : MonoBehaviour{ void Start() { //Set the tag of this GameObject to Player gameObject.tag = "Player"; } private void OnTriggerEnter(Collider other) { //Check if the collider of the other GameObject involved in the collision is tagged "Enemy" if (other.tag == "Enemy") { Debug.Log("Triggered by Enemy"); } }}
Additional Resources: GameObject.CompareTag, MonoBehaviour