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

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

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

simple as that really

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:
172349-screen-shot-2020-12-10-at-114237-am.png