< Return to Video

Tags - Unity Official Tutorials

  • 0:00 - 0:03
    Tags are a way of identifying game objects in Unity.
  • 0:03 - 0:06
    As a name of a single object could identify it,
  • 0:06 - 0:08
    it can be useful to set tags also.
  • 0:09 - 0:12
    For example you may have an object called Ork or Tank
  • 0:12 - 0:14
    but these could all be tagged Enemy,
  • 0:14 - 0:17
    and in your code you could check for any objects
  • 0:17 - 0:18
    that have the tag Enemy.
  • 0:18 - 0:22
    Likewise a script on an enemy could check for a player character
  • 0:22 - 0:23
    by looking for a player tag.
  • 0:23 - 0:26
    To assign a tag to an object, select it and use the
  • 0:26 - 0:29
    drop-down menu at the top of the inspector.
  • 0:31 - 0:35
    If the tag you want isn't already present then add a new tag.
  • 0:35 - 0:38
    You can add a tag by clicking the option at the bottom of the menu
  • 0:38 - 0:41
    and then entering it in the list of tags at the top
  • 0:41 - 0:43
    of the tag manager.
  • 0:43 - 0:45
    Once you've done this, return to the object you
  • 0:45 - 0:46
    wish to place the tag on
  • 0:47 - 0:49
    and select it from the drop-down.
  • 0:50 - 0:52
    There are a number of functions in code,
  • 0:52 - 0:55
    which will allow you to find objects with tags,
  • 0:55 - 0:58
    the simplest one of these is GameObject.FindWithTag,
  • 0:58 - 1:01
    which allows you to specify a string with the name
  • 1:01 - 1:02
    of the tag inside it.
  • 1:02 - 1:05
    This script is attached to my enemy object
  • 1:07 - 1:10
    and I can use that to find an object with the tag Player.
  • 1:11 - 1:13
    So I'll set my robot to be tagged Player
  • 1:16 - 1:20
    and when the game starts my enemy is seeking out that
  • 1:20 - 1:21
    object and looking at it.
  • 1:23 - 1:26
    Likewise you could find multiple objects with the same tag
  • 1:26 - 1:29
    by using FindGameObjectWithTag.
  • 1:31 - 1:34
    See the scripting reference for more examples of this.
Title:
Tags - Unity Official Tutorials
Description:

more » « less
Video Language:
English
Duration:
01:35

English subtitles

Revisions