How to Create and Use Tags in Unity
Notes:
How to Create and Use Tags in Unity :
A Tag is a "reference name or alias name" which we can assign to one or more game objects, so that we can refer them inside scripts and add similar behavior to them.
Note:
We can create any number of tags, and tags cannot be renamed
Viewing Tags & Layers Manager:
Edit menu – Project Settings – Tags & Layers
Creating new tag:
Click on Tags - Click on the + symbol - Name the tag
Assigning tags:
Select a game object - Click on the Tag drop down menu - Click on the required tag name
Referring tags inside scripts:
using UnityEngine;
public class PlayerController : MonoBehaviour {
void Update () {
this.transform.position = this.transform.position + new Vector3 (0f, 0f, 2f * Time.deltaTime);
}
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "Destroyable") {
Destroy (collider.gameObject);
}
if (collider.gameObject.tag == "MoveUp") {
collider.gameObject.transform.position = collider.gameObject.transform.position + new Vector3 (0f, 5f, 0f);
}
}
}