Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

Unable to resize or add Tags in tag field Unity 5.1 1 Answer

Set reference to multiple gameobjects based on tag category / multiple tags? 2 Answers

How to move a gameobject when it lands on a tag, to another tag? 0 Answers

A limit to the number of tags? 2 Answers

Match 3 game getting the colour of the match 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges