Hey, not a fan of having raw strings in my game code so was going to make a type for unity tags. I'm not sure how best to do this; as Enums cannot take string values - any recommended ways? I'd also like to be able to use it as a dropdown type for serialised parameters inside the unity editor if possible, to further reduce chances of my own user error breaking things
#Tags type/enum
1 messages · Page 1 of 1 (latest)
I once wrote a script that turns the tags defined in the editor into an enum (or rather, a bit mask).
My approach used a monobehaviour to contain the tag, but the auto generation script could be expanded to translate enum entries back to strings as well.
Interesting, thanks.
I'll take a look into this. Were you doing it for similar reasons as keeping things strongly typed?
My thoughts for turning it back into a string is just to ensure .CompareTag(Tags.tagA) still works as expected in code. Thanks again dude, will see what I can come up with!
I did it for multiple reasons:
- I wanted to avoid typos in tags
- I wanted to be able to assign multiple tags to one object
- For an increase in speed, as checking bitmasks/comparing enums is faster than comparing strings (although the overhead of GetComponent might take away from that)
- I wanted to see if it was possible to do that

Hello, if I understand you, you can use enum string. For example YourEnum.EnumText.ToString(). In result u get string enum. If u need int you can get this (int)YourEnum.EnumText
As for doing turning the enum back into a string, you won't be able to reliably do that with ToString, as the enum value names could have changed (since tags don't have to adhere to the same rules as C# member names).