Procedurally Generated 3D text mesh.

I’m trying to generate a text mesh with code. All I’m getting are blocks where the letters should be. When I drag the font on to my object from the inspector a Font Material gets created with that font. Then the text works. What do I need to be doing to fix this? I can’t just AddComponent(“fontMesh”). I know I can create a prefab but I wanted to do it this way.

public GameObject gm;
public Font _font;
public Material myFontMaterial;
public GameObject myTextObject;

// Use this for initialization
void Start () {
	_font = (Font) Resources.Load("Fonts/OCRASTD");
	gm = new GameObject ("Hello");
	gm.AddComponent("TextMesh");
	gm.AddComponent("MeshRenderer");
	gm.GetComponent<TextMesh>().text = "Hello";
	gm.GetComponent<TextMesh>().font = _font;
	gm.GetComponent<TextMesh>().fontSize = 40;
    //gm.renderer.material = (Material) Resources.Load("New Material"); //I tried to and a material here and that didn't work. 
}

Here is sample working code from my project. You don’t need to add MeshRenderer. I also think there is might be a problem with your font. Try to define for as public parameter you in your script and then set it via Editor. Also, you need to create material for particular font in editor and put to your Materials folder. Then pass material for desired font to a script and assign it.

            var textObject = new GameObject();
            var normalLyricsMesh = textObject.AddComponent<TextMesh>();
            normalLyricsMesh.font = Font;
            normalLyricsMesh.text = lyrics;
            normalLyricsMesh.alignment = TextAlignment.Center;
            normalLyricsMesh.anchor = TextAnchor.MiddleCenter;
            textObject.renderer.material = material;