• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Cambesa · Feb 22, 2017 at 02:05 PM · runtimetagtags

How to check if a tag exists?

Hey Everyone,

Im using

GameObject.FindGameObjectsWithTag()

to get a list of objects that have that specific tag, however not every tag used as input is a valid tag, it's a string that can be edited in the editor. I expected

FindGameObjectsWithTag

to return an empty array when there's no object found with a tag (which would be the case if the tag is not even defined) but instead I get this error message:

UnityException: Tag: is not defined.

It seems that it does not compute that method because the tag does not exists, the tag used to trigger this error is an empty string, I'm not sure if there's any difference between "" or string.Empty but it was the value unity uses when a public string is left blank in the inspector.

Is it possible to check if a tag exists or to get a list of strings of all tags set in the unity project? After browsing the forum I found a post from 2012 from someone who had a similar problem and fixed it by using an editor script. In my case however, it also needs to work on runtime so I can not use the editor script.

Kind regards,

Yorick van de Haar

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Nischo · Feb 22, 2017 at 02:14 PM

Something like this should work, while still being horrible style, and should not be called frequently

 public static bool DoesTagExist(this GameObject aObj, string aTag)
 {
     try
     {
         aObj.FindGameObjectsWithTag(aTag);
         return true;
     }
     finally
     {
         return false;
     }
 }

I made it an extension method so you can call DoesTagExist on any game object.

 bool doesIt = myGameObject.DoesTagExist("Player");

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cambesa · Feb 22, 2017 at 02:34 PM 0
Share

Thank you very much! The try/catch exception handling statement did the trick!

avatar image
1

Answer by thornebrandt · Jan 09, 2020 at 02:21 AM

The accepted answer wasn't working for me but I changed it slightly.

  public static bool DoesTagExist(string aTag)
   {
     try
     {
       GameObject.FindGameObjectsWithTag(aTag);
       return true;
     }
     catch
     {
       return false;
     }
   }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DSivtsov · Nov 13, 2020 at 12:03 PM 0
Share

Good variant in the Bad situation of absent correct method.

Little correction - changed "FindGameGame Objects()" on "FindGameGame Object()" . It will be more simple

avatar image Bonfire-Boy · Nov 13, 2020 at 12:15 PM 0
Share

The accepted answer should work but has done it as an extension method, so you need a GameObject to call it on, which is redundant and unnecessarily complicated.

So yes, yours looks better to me.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing tagged objects in script 1 Answer

How do I find grandparents with a certain tag ? 1 Answer

New to unity (player tag) 0 Answers

Why does CompareTag exist? 1 Answer

How can i get a list of tags from the tag manager? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges