Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Kartzan · Jul 19, 2011 at 11:01 PM · c#parentguitextdynamic

How to parent a GUI text to current game object

I am trying to do something slightly convoluted and my inexperience with Unity is proving my task to be very difficult.

I have several spheres in my scene and I want a GUItext to appear over each one when the player hovers the mouse over it. Essentially when the sphere is being hovered over I need to make the GUItext visible and parent it to the sphere that the mouse is hovering over, and I need the text the GUItext is displaying to match the name of the sphere.

All this could easily be done in the inspector, but unfortunately most everything in the scene is being created dynamically, so that throws a wrench into things. Any guidance would greatly appreciated.

Oh, and I'm doing all of this in C#.

Test

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

3 Replies

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

Answer by Eric5h5 · Jul 20, 2011 at 01:26 AM

You can't parent GUIText objects to anything, because they use viewport space, whereas 3D objects use world space. (Well, technically you can parent them, but it's useless because of the incompatible coordinates.) Use a script with WorldToViewportPoint (see here for an example).

Comment
Add comment · Show 8 · 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 kaleetos · Jul 20, 2011 at 01:47 AM 0
Share

Very true! I was thinking of 3d Text when I left that comment; my bad.

avatar image Alec-Slayden · Jul 20, 2011 at 02:00 AM 0
Share

3D text isn't a bad idea though, since it can be parented to allow natural movement tracking and rotation, and the size comes natural with perspective.

avatar image Eric5h5 · Jul 20, 2011 at 02:08 AM 0
Share

You rarely want text labels to be affected by rotation, size, etc., because they frequently aren't readable that way.

avatar image Kartzan · Jul 20, 2011 at 11:48 AM 0
Share

Thanks for the help guys, I've gotten most of the kinks worked out, currently slim$$anonymous$$g down WorldToViewPortPoint so I only use what I need to. Its a really handy script that I had no idea existed.

avatar image Eric5h5 · Jul 20, 2011 at 09:03 PM 1
Share

Yes; the script uses Transform variables.

Show more comments
avatar image
1

Answer by thekoop · Jul 20, 2011 at 12:37 AM

If you're doing it in script, you can just use transform.parent = TheGameObject.transform

simple as that really

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 Kartzan · Jul 20, 2011 at 01:01 AM 0
Share

But if the script is attached to the sphere, wouldn't that make the sphere the child of the GUItext?

avatar image kaleetos · Jul 20, 2011 at 01:14 AM 0
Share

when you instantiate the GUIText in the sphere's script, assign the resulting object to a variable. Then set that objects parent to be the sphere:

variable = Instantiate(TheGUIText);

variable.transform.parent = transform

avatar image
0

Answer by Blendwolf · Dec 10, 2020 at 05:47 PM

You can vary easily just change the canvas render mode from "screen space" to "world Space" when the canvas and text are attached under the game object: alt text


screen-shot-2020-12-10-at-114237-am.png (40.0 kB)
Comment
Add comment · 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

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

8 People are following this question.

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

Related Questions

Rotate similar child objects around own origin 2 Answers

Distribute terrain in zones 3 Answers

Destroying an object from its script. 1 Answer

Script to add script to all children 1 Answer

Make dynamically a list of GuiText's 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