All The Tags!
Asset Store ($10) | Documentation
All The Tags! Replaces the built in tagging system in Unity3D. It’s sole purpose is to allow you to add more than one tag to any object, however there other benefits too! It performs substantially better than doing a standard string compare, generates no garbage and allows you to customize your tags with colours to stay organized!
Screenshots
FAQ
The documentation mentions that the AllTheTags methods never return null? What do they return if it doesn’t find any objects?
Click for answer
It just returns an empty collection of objects. This way you don’t have to do null checks everywhere.
var taggedObjects = Tags.FindGameObjectsWithTag("MyTag");
foreach(var taggedObject in taggedObjects)
...
Instead of
var taggedObjects= Tags.FindGameObjectsWithTag("MyTag");
if(taggedObjects!= null)
{
foreach(var taggedObject in taggedObjects)
...
Do I need to ensure every GameObject has an AllTheTags component before I pass them along to an AllTheTags function?
Click for answer
No - AllTheTags is very rebust - if an object does not have an AllTheTags component, it will add it for you when you try to check that GameObjects tags. It won’t throw any errors at you.
So you could safetly do Tags.HasTag/etc on any object in your project. Whether they have an AllTheTags component or not doesn’t matter!
Simply add the ‘AllTheTags’ component to any object that needs one via the inspector, and the rest will handle itself
If an object does not have an AllTheTags component, it is considered to have ‘no tags’.
Tags.FindGameObjectsWithTag(string tag) - how exactly do I use it?
Click for answer
By default, the FindGameObjectsWithTag returns a ReadOnlyCollection of objects. This is just like a List except that you can’t add objects to the list or remove objects from it! The reason it is done this way is because most of the time all you want to do with the result of FindGameObjectsWithTag is iterate over all of the objects and run some code on each object. A ReadOnlyCollection is fine for this task, and it means that we don’t have to allocate a new List of objects in memory to hold the result of every FindGameObjectsWithTag operation. Nice and efficient.
You can iterate over a ReadOnlyCollection with a foreach statement, exactly the same as List.
If ReadOnlyCollection doesn’t fit your requirements however, you can still get a normal List out of the results fairly easily so you can do whatever you like with the results!
List<GameObject> myList = new List<GameObject>( Tags.FindGameObjectsWithTag("MyTag") );