- Home /
How do I attach text to a gameobject?
If I understand correctly, any UI element (including text) must be the child of a Canvas Object in order to be drawn.
I am instantiating a prefab GameObject from a script and want to attach some text to that object at run time. What is the best way to accomplish this?
The easiest way is probably to just use a Text $$anonymous$$esh. It's a 3D object that functions just like any other GameObject, and you can set it as the child of your instantiated object. You might want to place a white Quad behind it to actually see the text, as black text on dark backgrounds isn't the easiest thing to see.
Otherwise, you'll have to figure out the screen-coordinates of the instantiated object, and move the text to those coordinates. You'll need to use Camera.WorldToScreenPoint to get those coordinates.
Answer by Abion · Apr 24, 2016 at 02:42 AM
This is an old question, but I was looking into this a bit and discovered a solution. You can set the Canvas' render mode to World Space, you can then set it as the child to a GameObject and the Canvas will appear at that position in the world.