- Home /
How to get a list of gameobject tags in runtime
I'm trying to check to see if a string is a gameobject tag but I can't immediately find a way to access the list of gameobject tags in runtime. There is this property UnityEditorInternal.InternalEditorUtility.tags
but this is editor code and does not build so it can not be used. I really thought there would be something like GameObject.tags
or GameObject.GetAllTags()
The other option I have tried is doing something like bool isTag = GameObject.FindGameObjectWithTag("my-tag") != null
but this only works if there is a gameobject in a scene with that tag. I may not be in that scene yet as this is all happening in an asynchronous function.
Has anyone ran into this issue as well and shed some light?
why do you need to check if a string is a tag? tags are for finding gameobjects and in that case one should know the tag name. if you want to do something different with tags, maybe you should look for different solution.
Because I'm making an app navigation script using URLs. Something like "scene/screen/some-info". And I'm tagging the UI screens in the scene because it seems like the best way to go about it. I have found a work around for now but I was wondering if anyone has had this issue. Right now I'm just waiting for scene to load first then call the .FindWithTag()
function. I guess its fine because I had to wait for the scene to load anyway. But it does seems weird that I can't access a list.
Answer by DevinW · Feb 12, 2018 at 07:39 PM
You're better off not using tags or any form of string literals.
You can make a class that does a dependancy injection
into some manager class that stores it in some list or dictionary. On the object's awake you'd reference the manager class and send an event to it that registers the item with it.
If this doesn't sound like a solution to what you're trying to do you can look into this http://blog.almostlogical.com/2014/03/11/tags-layers-and-scene-constants-generator-in-unity/
It's a simple tool that generates all the strings you'll need for tags, layers, and scenes.
That is quite smart. I like this approach. I was thinking of doing something similar in a build script, but this just saved me hours. Thank your sir.
Your answer
Follow this Question
Related Questions
difference between GameObject.FindGameObjectWithTag(Tag tag) and GameObject.FindWithTag(Tag tag) 3 Answers
Need help with C# code. both gameObject and col.gameObject are getting destroyed. here is the code. 2 Answers
Deleting gameobject with tag problem 1 Answer
How to resort to another object if one is not found? 1 Answer
Hi, getting error cs1041 in my code:identifier expected, "this" is a keyword; need solution please 3 Answers