Unity Tags vs Layers. When to use Which?
Both are used to classify objects of a scene, but Unity does use layers in a functional way where tags are meant to be used by scripts only. Functional how? For instance, layers can be used to specify a set (category) of objects to be rendered, layers can also be used to ignore collisions or raycasts and more.
There is some overlap of usage between them, you can certainly get away with using layers only, but if you want to further categorize your game objects you can assign tags to them.
An object can have no more than a single tag. Unfortunately.
Common uses for Layers
Layers are used (among other things) to selectively render objects. For each object you assign a layer, then on your camera, create a culling mask by choosing the layers that will be rendered by this camera. The end result is that only objects assigned to those layers will be rendered by that camera. But why is this useful? You could be creating a mini-map, could be used to render a 3D UI on top of the game world render. Selective render is a type of culling.
While still in the realm of rendering, layers can be used to specify which objects should be affected by select light sources, it uses the same idea from the previous paragraph, you create a culling mask by selecting different layers in the Culling Mask property of the light you're working on. This can be done to get your rendering right in the aesthetic point of view as well as performance reasons. To understand why there are performance considerations please see this article from Unity on choosing a rendering path.
Layers are also heavily used to avoid unnecessary collision checks. Let's say you have enemies and would like to know if they collided with your player's character weapon, if you leave all your game objects in the same layer (usually the Default layer) you'll end up doing a lot of collision checks that you don't need, so it's wise to create a Combat layer on that case and assign only the objects that should be affected by these collisions to this new layer. For more information on layer based collision see the official documentation manual page on that.
Finally, layers can be used to avoid computations while doing raycast or overlap checks.
- Selective render (culling mask)
- Only light certain objects
- Ignore collisions
- Ignore objects in raycast or overlap checks
What about tags?
Tags in other hand don't offer you any engine functionality. You purely use them as scripting data, not demeaning their use, they are easy to use and powerful. For multiple objects in the same layer, you could use tags to differentiate between them while deciding what to do in the result of a collision, for example.
Different from layers, tags offer some helper methods like FindGameObjectsWithTag which can be pretty useful to do initializations.
They are pretty different
Layers are fundamental in skipping unnecessary checks and other computations, which yield the maxim
never leave all of your objects
in the same layer, this is a really rare situation to happen in a game, always have in mind of what you need
for your game and do the least amount of work to achieve the desired result.
Tags aren't strictly necessary but can be very helpful.
I hope that was helpful, and as always, you can find me on twitter [email protected] for questions, opinions or anything else, let’s chat!