OK, this is just a random, open-ended question for you Unity pros out there… I understand the benefit of having tags and layers for Unity scenes, and have already used them for some good results. However, it seems to me that tags and layers are pretty much identical, at least how I’ve used them so far.
So, here’s my question… when is it better to tag objects as opposed to putting them in layers? What can one do that the other can’t?
You can tag things all you want as you’ve got a practically unlimited number of tags. However, you have a limited number of layers. Also, layers can be used to determine which objects are rendered by cameras and which objects are ignored during raycasts. Tags can’t do that.
You can’t cull objects for cameras, raycasts, etc. using tags, but must use layers for those. There are 32 layers, but a (more or less) infinite number of tags, since they are strings, whereas layers are bits. Actually I’m not sure how they could be used in an interchangeable way? FindGameObjectsWithTag, for example, has nothing to do with layers.
I guess my confusion comes from the fact that I’ve been mostly using tags and layers through things that apply to both. For instance, when two objects collide with one another, it’s just as easy for me to find what the tag is of the colliding object as the layer, and make my game react accordingly.
Basically, what I’m getting from this is that layers have more functionality, but there can be an infinite number of tags.
I suppose a good example of how to use layers and tags properly would be to have a layer called “Enemies”, but each different type of enemy has its own tag… “Robot”, “Crocodile”, “Fire Monster”, “Rancid Cheese”, etc.
Well…layers are for visual/physical effects, so I’m not sure that’s a good example. However, you’re limited to one tag per object, so that example would be one way to apply two different “tags” to the same object, but I’d call it a hack. Not that it’s necessarily a bad idea, but it’s not really what layers are for. On the other hand, if you were planning on using that “Enemies” layer for some purpose such as having an overlay screen where only enemies show up, or something like that (use a camera where all layers but “Enemies” is culled), then that would be a typical use for layers.
I would actually handle such kind of stuff through a MonoBehavior called CharacterEntity where the class / race is one of the properties.
then you can always just go for that component and are fine.
the benefit of that is that you can make the ai stuff part of that component for example
GameObject has a GetComponentsInChildren method that retrieves all components of a particular type in all an object’s children. If you had all enemies as children of a containing object, say, then this would be an easy way to get all the scripts at once.
However, you probably shouldn’t worry about whether you are using layers, tags or whatever as long as the technique works well for you. As Eric said, the reason for the existence of layers is largely about layer masks - the implementation of these is more efficient if only a small number of them is allowed.