Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by MapuHoB · Dec 05, 2014 at 06:09 AM · c#ui

Find out if gameObject is UI in 4.6

How can I easily and fast find if a certain gameObject is UI or not ?

Comment
Add comment · Show 1
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 Lunatix · Dec 05, 2014 at 09:37 AM 0
Share

You could check if the game object resides in the UI layer, tag it with "UI" and check against it or even assign a specific name to all your UI objects.

1 Reply

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

Answer by HarshadK · Dec 05, 2014 at 07:47 AM

Check if the GameObject contains a RectTransform component on it because UI element game objects have a RectTransform component attached to it as opposed to other objects which have a Transform component.

 if(gameObject.GetComponent<RectTransform>() != null )
 {
     // This is a UI element since it has a RectTransform component on it
 }

Comment
Add comment · Show 4 · 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 Kiwasi · Dec 05, 2014 at 08:44 AM 1
Share

This will work after a fashion. But there is no reason a RectTransform can't be attached to a regular GameObject.

A better way might be to check for components from the unity engine namespace.

An even better question is why? There is no inherent difference between a UI GameObject and a regular GameObject. As per normal unity rules a GameObject is defined by the components attached to it.

avatar image HarshadK · Dec 05, 2014 at 09:12 AM 1
Share

@Bored$$anonymous$$ormon Nice point. I actually didn't think on the fact that user can actually add a RectTransform to regular game object making this method less usable in such instances.

Then in this case a better method I could think of is checking for CanvasRenderer since you won't add a Canvas Renderer to a game object unless you want that object to be a UI element. As there is no CanvasRenderer attached on Canvas itself one can check for Canvas component itself. I guess this will make it better than RectTransform. This method is just an extended step of previous method and still has the case of what if user applies a CanvasRenderer to a regular game object.

Or one more precise method would be to check for presence of particular UI element components from UI namespace as you have specified. But I guess it would be a little bit daunting to check for possible values under UI namespace.

avatar image Kiwasi · Dec 05, 2014 at 09:26 AM 1
Share

Should be able to do it with reflection. It all depends on the use case. In 99% of cases the RectTransform method will work. Perfectly fine if you are building your own game. But probably a little rough for something destined for the asset store.

avatar image HarshadK · Dec 05, 2014 at 09:34 AM 0
Share

@Bored$$anonymous$$ormon can you post a method with how to achieve this with reflection. I would be happy to see it. I'm a bit weak with reflections. :-(

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to bold only the selected characters from a text? 3 Answers

How do I save and load the value of a text? 1 Answer

Change scene and play it by clicking UI Button? Scene does just load but not start playmode? 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