Create a textmesh in c#

I’m trying to create and display a textmesh in c#.
All the answer I have found have been javascript replies and I’m not good enough to covert between the two.

So far I have:
`

using UnityEngine;
using System.Collections;

public class CreateText : MonoBehaviour {



	public Material mat;
	public Font font;

	void Start () {


		// create 3d text mesh
		GameObject Text = new GameObject();
		
		TextMesh textMesh = new TextMesh ();
		Text.AddComponent (textMesh);
		textMesh.font = font;
		MeshRenderer meshRenderer = gameObject.AddComponent(MeshRenderer);
		meshRenderer.material = mat;
		
		textMesh.text = "Hello World!";

		}
	
}

Any ideas ?

`

What you’ve got there looks pretty good to me; it does seem to create a gameobject and add the same components to it that a textmesh has by default. If it’s not working I suspect it’s because you haven’t set up the font or the material that renders the mesh properly. That info is hard to glean from your code samples, as we cannot know what you’ve dragged onto those properties in the editor.

Instead of using this approach, however, I suggest you create a 3D Text object in the editor (GameObject->Create Other->3D Text) and then configure it so it does what you want. Then create a prefab out of that prototype, and from script, instantiate copies of that prefab. That’s probably a more stable approach than trying to manually piece together all the components yourself.

Assuming you have a 3D Text prefab, you can access and modify its text like in the code below. It’s an example, but it’ll work just fine:

    Transform txtMeshTransform = (Transform)Instantiate(yourTextPrefabHere);
    TextMesh txtMesh = txtMeshTransform.GetComponent<TextMesh>();
    txtMesh.text = "New text set through script";
    txtMesh.color = Color.red; // Set the text's color to red