x


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

more ▼

asked Jul 19 '11 at 11:01 PM

Kartzan gravatar image

Kartzan
1 6 6 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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).

more ▼

answered Jul 20 '11 at 01:26 AM

Eric5h5 gravatar image

Eric5h5
80.2k 41 132 519

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

Jul 20 '11 at 01:47 AM kaleetos

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.

Jul 20 '11 at 02:00 AM Alec Slayden

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

Jul 20 '11 at 02:08 AM Eric5h5

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

Jul 20 '11 at 11:48 AM Kartzan

Yes; the script uses Transform variables.

Jul 20 '11 at 09:03 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

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

simple as that really

more ▼

answered Jul 20 '11 at 12:37 AM

thekoop gravatar image

thekoop
24 1 2

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

Jul 20 '11 at 01:01 AM Kartzan

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

Jul 20 '11 at 01:14 AM kaleetos
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4151
x3327
x422
x313
x232

asked: Jul 19 '11 at 11:01 PM

Seen: 2696 times

Last Updated: Jul 20 '11 at 09:47 PM