Create a 3d text using c# (font problem)

Hello there !

I am trying to create a 3d Text using c#, but i have trouble with the fact that instead of text, it displays a pink square. The problem, in my opinion, is that it doesn’t have any texture, because it doesn’t have any font. Here is the code :

		GOTroopsCountIn = new GameObject("TroopsCount");
		GOTroopsCountIn.AddComponent("TextMesh");
		GOTroopsCountIn.AddComponent("MeshRenderer");
		TextMesh TMTroopsCountIn = (TextMesh) GOTroopsCountIn.GetComponent("TextMesh");
		TMTroopsCountIn.font = ??????????? ;

So here is my question : how can i find a font to assignate it to my TextMesh (using c#) ? (The default font, Arial)

Have a nice day !

The pink squares are what you get when you don’t have a material assigned, or if your shader has errors. I use EZGUI rather than TextMesh, so I had to play a bit to get something to work.

I dragged and dropped a TTF font into the project. In order to get the letters to appear on the texture associated with the font, I had to change the font import settings ‘Character’ property to ‘Unicode’. Then I followed the instructions here to create a shader and material:

http://wiki.unity3d.com/index.php?title=3DText

Next I attached the following script to an empty game object:

var mat : Material;
var font : Font;

function Awake() {
    var Text = new GameObject();
 
    var textMesh = gameObject.AddComponent(TextMesh);
    textMesh.font = font;
    var meshRenderer = gameObject.AddComponent(MeshRenderer);
    meshRenderer.material = mat;

    textMesh.text = "Hello World!";
}

I dragged and dropped the font and the material onto the public variables and hit play.

This seems easier. No custom shader, unless you needed the shader for some other reason. A Font object includes Font.material, and when you add a TextMesh it includes the render, so you can skip some steps and just do:

var font : Font;
 
function Awake() {
   var Text = new GameObject();
   var textMesh = Text.AddComponent(TextMesh);
   textMesh.font = font;
   textMesh.GetComponent<Renderer>().material = font.material;
   textMesh.text = "Hello World!";
}