-
Tags are a way of identifying game objects in Unity.
-
As a name of a single object could identify it,
-
it can be useful to set tags also.
-
For example you may have an object called Ork or Tank
-
but these could all be tagged Enemy,
-
and in your code you could check for any objects
-
that have the tag Enemy.
-
Likewise a script on an enemy could check for a player character
-
by looking for a player tag.
-
To assign a tag to an object, select it and use the
-
drop-down menu at the top of the inspector.
-
If the tag you want isn't already present then add a new tag.
-
You can add a tag by clicking the option at the bottom of the menu
-
and then entering it in the list of tags at the top
-
of the tag manager.
-
Once you've done this, return to the object you
-
wish to place the tag on
-
and select it from the drop-down.
-
There are a number of functions in code,
-
which will allow you to find objects with tags,
-
the simplest one of these is GameObject.FindWithTag,
-
which allows you to specify a string with the name
-
of the tag inside it.
-
This script is attached to my enemy object
-
and I can use that to find an object with the tag Player.
-
So I'll set my robot to be tagged Player
-
and when the game starts my enemy is seeking out that
-
object and looking at it.
-
Likewise you could find multiple objects with the same tag
-
by using FindGameObjectWithTag.
-
See the scripting reference for more examples of this.