Tag identifies individual, Layer groups similar.

There are three potentially confusing concepts in Unity: Layer, Tag and Sorting Layer. When asked about their difference, many would say Layer is for physics and Sorting Layer is for rendering. When writing a condition check in OnTriggerXXX, many would just choose whichever comes to their mind first, be it Tag or Layer.

In this post, you will see that the Physics/Rendering dichotomy is not really valid, and being inconsistent in using Tag and Layer is really a bad practice.

Tag

Let us starts with the easiest of the three.

Tag is the easiest to use simply because it is designed to be so. Unlike Layer and Sorting Layer, Tag never interfaces with Unity Engine feature. There is no component that would need to check Tag of an GameObject in order to do something, and there is no API that takes in or output an Tag (in fact, there is no dedicated Tag class. It is just a string), except for those designed to search Tag. Since Tag is not a building block of the whole Engine architect, I infer the only reason why they are present is that they are added to facilitate better, faster programming.

Unity Manual echoes what I thought.

Tags help you identify GameObjects for scripting purposes. They ensure you don’t need to manually add GameObjects to a script’s exposed properties using drag and drop, thereby saving time when you are using the same script code in multiple GameObjects.

Unity TechnologiesTags

One suggested use of tag mentioned in the Manual (which is also ubiquitous in a lot of beginner tutorial) is to use them as a check in OnTriggerXXX methods, to check “whether the player is interacting with an enemy, a prop, or a collectable, for example.

Another use is in methods like GameObject.FindWithTag() and GameObject.FindGameObjectsWithTag(). The former returns an arbitrary gameobject with the specified Tag, and the latter return all.

In general, tags should be used to:

  • Give logical, gameplay-level, player-perspective grouping to gameobjects, instead of engine-level, programmer-perspective groupings. This allows us to search through the scene to find specific groups of gameobjects or check collision among objects in the same layer.
  • Give a unique label to individual gameobjects so we do not need to find them by name (GameObject.Find()).

Some tips:

  • Do not write if (collider.tag == "Player"); Use if (collider.CompareTag("Player")) instead for best performance.
  • Tag string is really “anything goes”. There does not seem to be word ban or character limit.
  • There can be infinite Tags, but a gameobject can have only one tag and must have one tag (If you do not tag it, it has the Tag “Untagged”).
  • Tag is optional. You do not have to use it.

Layer

Unlike Tag, Layer is heavily built in the Engine. There are many components and functions that interface with Layer, not just physics ones. I list some on top of my head:

  • Physics-related
    • Selective collision.
    • Selective cast and overlap check.
  • Rendering-related
    • Camera selective rendering (culling mask)
    • Light and Light2D selective illumination

Layer is really implemented as a bitmask - so that is why there can only be 32 layers at most (Moreover, Unity occupies a few slots by default). However, by virtue of bitmask it is permissible for LayerMask to include more than one Layer, although an gameobject can still only have one Layer.

In general, tags should be used to:

  • Provide groupings for collision. For example, I create separate layers for enemy and player bullets and enemy and player bodies to prevent shooting bullets that kill oneself.
  • Provide groupings for lighting target. For example, I want my player not to be lit by the light source emitting from herself.
  • Provide groupings for gameobjects that might be rendered or hidden in different situation. This is used to separate the game and GUI elements. The UI layer, which is there by default, is automatically assigned to all UI elements upon their creation.

Tips:

  • If you want the raycast to check for only gameobjects in the first layer, you write int layermask = 1 << 1(Not 1 << 0!). If you want everything but the first layer, simply do layermask = ~layermask.
  • Layer 31 is used internally by the Editor’s Preview window mechanics. To prevent clashes, do not use this layer.
  • At the start, clear out all checks in the collision matrix except for the row of Default.

Sorting Layer

Sorting Layer is purely about rendering. On top of my head, there are at least two rendering components that allow you to assign a Sorting Layer to the things they render: Sprite Renderer, Tilemap Renderer.

Sorting Layer is the most prioritized factor when Unity decide which out of the few overlapping graphics should be in front of which. It is used usually with Order in Layer to specify an internal ordering within Layer. There are various other factors that determine the order of rendering when both Sorting Layer and Order in Layer are the same.

Sorting Layer, in fact, has a very clear-cut use and is distinct from the above two. It is included here only because it is often confused with Layer - majorly because of the similar nomenclature (perhaps we should really have a better name for Sorting Layer - Perhaps “Sorting Level”?).

Tips:

  • Start with the player/enemy Sorting Layer and think about what would go in front of and behind these characters.