1
\$\begingroup\$

How do you incorporate Tag strings into a scripts editor found in the inspector?

What it is I'm babbling about: When you create a public GameObject fooObject in a script, it shows up as a box that can have Game Objects dragged onto in its editor. It also has a small clickable dot that shows every Game Object that could be selected.

Now, say I have a public string fooString that would specifically be used for referencing the Tag strings of Game Objects (such as if (fooObject.tag == fooTag) or another use). Is there a way to define that string so that in the script's editor it strictly allows tag names to be used with it? I'm assuming with a similar clickable dot or a drop down menu of every selectable tag.

Although I could just manually type my string, a solution like the one above would have big object-orientated advantages. For example, if I was to change the name of this tag, it would change the string variable accordingly, as if the variable was a reference to such tag.

[Note to the moderators: I looked for an answer to this question for nearly 2 hours now to no avail. I understand that I may be missing some specific wrong word choice, in which case this question easily could have already been answered here. If this is closed as a duplicate, then, a quick comment explaining my mistake would be greatly appreciated.]

\$\endgroup\$
3
  • \$\begingroup\$ Since you can change the tag at runtime, why worry about the editor? Just set an enum you can set in the editor and then set the tag approrpiately when your object enables. Would that work? \$\endgroup\$ Commented May 16, 2015 at 0:56
  • \$\begingroup\$ That would work, in fact its my current workaround. But ideally if I was to change the name of this tag, it would change the string variable accordingly, as if the variable was a reference to such tag. Question edited accordingly \$\endgroup\$ Commented May 16, 2015 at 1:06
  • \$\begingroup\$ I understand better. Is there some other Type you could get instead of GameObject? For example if they are Ships, then public Ship fooShip. Then you could get the gameobject or components of that ship as needed from the reference. And it would only take objects of the appropriate type. \$\endgroup\$ Commented May 16, 2015 at 1:30

1 Answer 1

2
\$\begingroup\$

I'm not really sure why you need this, or how exactly will it optimize your script, and I can think of several ways you can handle this in a much more efficient way without need of all these, but here goes :

EditorGui.TagField gives you a selector for choosing a tag.

UnityEditorInternal.InternalEditorUtility.tags gives you an array of all the Tags in your Build, which is probably what you need.

\$\endgroup\$
1
  • \$\begingroup\$ It would save time editing. If I decide to change a tag's name now, I don't have to run through every script that uses it and change the names to match. \$\endgroup\$ Commented May 19, 2015 at 20:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .