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?

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.