(SOLVED) Drawing text above game objects generated at runtime

(SOLVED: c# - How to procedurally attach text to generated gameobjects? - Stack Overflow)

Hello! I am drawing some cubes in my unity scene like so:

foreach (Cube cube in cubeList)
{
   if (cube.canStandOn)
   {
      GameObject block;
      block = GameObject.CreatePrimitive(PrimitiveType.Cube);
      block.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
      block.transform.position = new Vector3(cube._mPos.x + CubeSize.half, cube._mPos.y + CubeSize.half, cube._mPos.z + CubeSize.half);
      block.GetComponent<Renderer>().material.color = Color.blue;
   }
}

I dont really like that solution because it creates alot of game objects which is annoying but whatever…
Now I wanna draw text above these cubes with that states the cubes positions in my grid (might be different each run) so this must be done at run time programatically

but I dont get how to do that… any tips?

also if you got any tips on how to draw these cubes without creating a bunch of game objects that would be nice as well ^^

OH! And this is kinda… for an android app as well so certain functions like ondrawgizmos wont work ^^:::

This is how I want it to look: (I diddnt add text for all cubes but they should all have such a text with their position)

can try this for simple text

Or unity 3D Text, or unity UI (move UI texts over world gameobjects)

Are you making minecraft’ish terrain or how many cubes you will have?

Not making terrain. Might have Many cubes or very few depends on the size of my meshes which are genrated at run time.

Might add: this must be done at run time programatically I cannot have pre made text objects and attatch them to my cubes which is the case for most tutorials :confused:

also I might have a bunch of these texts (several 100) and they might change every run.

tried this:

but did not get it to work… it seems you are not supposed to create a text mesh (or 3D text) at runtime since they want you to have text mesh instatiated before on your game object but my objects are created at run time.

the new UI would let you add worldspace canvas to each of the cubes. Prefab would be simplest but you could add the entire thing through code if needed.

aha that sounds good. Got any example on how to do that with code? All example I see with UI is done in the unity editor :confused:

Drawing on text GUI could have been a nice solution but I dont want this in game window just scene window for debugging… I need to know the position of the cubes I generate and draw when I hit play… why is it so difficult to draw procedural text in unity anyway? :stuck_out_tongue:

Ok I have looked everywhere I could think of and it seems unity dont like you drawing 3D text procedurally at all… It should be as simple as creating those cube primitives but it seems it really isnt…

If you don’t want to see it in game view,
can create them on specific layer and then hide that layer from main camera.

You can adjust text in unitystbEasyFont and text mesh,