Multiple tags

Is there a way to have multiple tags for a GameObject ?

Inside the GameObject adds for example 4 boxes, each with different tags and the same size of the collider. a unique object, in this case a GameObject with the null object and 4 boxes with different tags.

I think that will help.

Unfortunately, no. The tag property of a GameObject is defined as a simple string with a setter implemented that checks its value being set to one of your previously defined tags.

I think pauloaguiar was suggesting adding multiple child GameObjects with different tags that you could use to simulate multiple tags. Depending on your requirements I would consider implementing your own custom tag system to enable multiple tags. Its not too difficult, but it depends on what you need to use them for.

you could do a nasty hack…

make-up a tag like “enemy,fire”

do some string split action.

multiple tags implemented in one nasty as $hit hack!

Is it really that nasty? Instead of tag == “x” you use tag.Contains(“x”) and you’re good to go, right? Adding to a tag is easy as it’s identical to the existing + operator. Removing is a little more difficult, but could probably be done simply enough with Replace(…). I’d make nice wrappers for each operation, and the underlying data structure really doesn’t matter. I don’t know how well it’d play with the built in tag manager, though, as I very rarely use tags myself.

You have to consider what’s worse. The above, or having a redundant system as a result of rolling your own from scratch instead of extending what’s there. Both have pros and cons, though personally I think I’d go with extension instead of replacement.

Very true. However, in this case, the system isn’t particularly extensible. Unity’s implementation will not allow the setting of a tag to a value that isn’t actually registered as a tag - to allow for all possible combinations you would have to register all possible combinations in Unity, where even a different order in the substrings would make a different combination. Even if that were feasible you’d still end up breaking the “find by tag” methods for querying a containing tag (it would have to be it’s exact permutation).

Just FYI - like you I don’t use Unity’s tag system either.

A collection of static Dictionaries that would register/unregister an object on their Awake/OnDestroy methods with the proper maintenance I’ve found works well (so far for me anyways). And if the tags are defined once at the project level you can simply use enums instead of strings to avoid relatively expensive string comparisons and gain some compiler validation to boot. Some issues do arise when modifying them and trying to preserve previous values through the serialization process, but that can be managed as well.

Its nasty in the fact you need to create multiple tags for slight variations of the same things…

enemy,fire
enemy,fire,boss
enemy,fire,boss,super
enemy,boss
enemy,ice,boss

etc

Personally I prefer to use name since name can be set dynamically and its just as easy to access as tag.

these days, a spawned vehicle for me looks something like:

Team1.tank-01.23423434.blah.etc

This way, not only are all the vehicles/players/etc put together in the scene view, they have meaningful details so I dont have to go selecting the gameobject, finding a script value to check what/who they are.

I’d say that is probably a bloated sollution. What is it you need multiple tags for OP?

I was just wondering how to do it. In C++ I would use enum for those things.

You still could use an enum, right? You’d have to explicitly cast it to an int for some things, but I think that’s the only difference.

Yeah, this was the bit I was unsure of, having not used the tag system much myself. In fact, I have a feeling that this is precisely why I never used it much.

What I typically do is use components, purely because it’s already there and happens to suit my requirements and style in a logical manner. Rather than checking ‘other.tag == “Player”’ I instead check if a Player component is present. I’ve simply never had occasion to check for something that doesn’t have a relevant component on it anyway, and if the component is there I’ll want to access it.

Im pretty much the same. I do use tags, but only a small handful.

The main tag I use is Team1…Team4 so that I know which team a player is on.

I have yet found a need for multiple tags on one item.

Yeah, I wouldn’t even use a tag for that. I mean, there’s no reason it wouldn’t work, but in my style if something can logically be on a team it’d already have a relevant Component on it, and I’d find out the team from that component. That’d be slightly slower than checking a tag, but that’s not an issue with event based code.

I guess I just have an aversion to tags because they have no context in and of themselves, where any data attached to a component has a pretty explicit context. For instance, if the tag on an object is “Blue” that doesn’t tell me much. But if a “team” variable on a “Player” component is set to “Blue” it’s pretty clear what that means, as is a “colourName” variable on a “Paint” component. There’s nothing necessarily wrong with that, it just doesn’t suit my style.

while im generally the same (using components/event systems), I found the tag system was good for fast out options… especially in overlapsphere ops.