Tutorial: Multiple Tags in Unity 3d

I have often found the need to have multiple tags in Unity, but found Unity is limited to only one tag per object. After researching what others did I wasn’t very happy with the solutions being presented. Many would add multiple empties to an object and have each empty contain a different tag. Then when searching they would have to look through all the different empties. I personally thought this too cumbersome.

So what I do instead is I create a custom class “CustomTags” in which I can put as many tags on a game object as I want. I have these tags as an Array of strings, giving me more flexibility but you can also use enums as well if you would like. I then have a function in this class that simply searches through the array to see if the desired custom tag exsists or not.

Then whenever you need to check a game object for a specific tag instead of using

You instead write

If you are checking an object without the class CustomTags you will get an error thrown, so you either need to be careful not to check unless you are using CustomTags, or just check if the game object has the component or not first.

Hope this helped, feel free to give feedback below.

 

You may also like

2 Comments

    1. You are the man Bryan. On the long road of coding the hardest thing is not knowing what you don’t know. That is so much easier!

Leave a Reply

Your email address will not be published. Required fields are marked *