- Home /
Define a string variable as a tag? (C#)
First I define a string:
public string floorTag;
And later I say that something with the tag "floorTag" should do something:
if (hit.transform.tag == floorTag)
The problem is that this does not work unless I define "floorTag" in the editor (the actual name of the tag I want is "Floor"). How can I define the ambiguous string floorTag as the tag, so that it doesn't even show up in the editor?
Answer by MrsZorx · Jan 10, 2014 at 09:48 AM
Looking at this answer: http://answers.unity3d.com/questions/33597/is-it-possible-to-create-a-tag-programmatically.html
It does not appear possible:
"Tags must be declared in the tag manager before using them"
Maybe if you explain why you wanted floorTag to be ambiguous we could help with that? There's nothing to stop you changing the tag in code eg.
hit.transform.tag = "Floor"; //where "Floor is defined in the editor"
If it's something where you'd end up with too many types/alterations of floorTag maybe you should keep the simple "Floor" tag in the editor as above to narrow is down and then consider a different variable storing its' more specific id that you can query after.
whoops, did not mean to write ambiguous tag, just meant tag. Does that make it simpler?
If you add "Floor" as a tag in the editor you don't actually have to use it in the editor. You can still wait until your code to actually apply the tag to anything (by doing mything.tag = "Floor";
). But the way unity is set up you can't create tags in the code, but you can use or assign existing tags that you've already defined in the editor as much as you like. If you find after you created the tag in the editor that it automatically applied to an object, which you didn't want, just select the tag drop down on that object and change it back to Untagged. This does not delete the tag and it will still be usable in code.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
C# Variable 4 Answers
Can i give GetComponent a variabel instead of a scriptname? 1 Answer