Creating a simple 3d text in C#

I seem to run into a little snag when trying to create 3dtext.
I’ve copied a 3dtext script but its missing the Mesh Renderer part, the material to be precise.

The script so far in start() :

void Start () {
				
		Font ArialFont = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");    

		GameObject myTextObject = new GameObject("test");
		Vector3 tempPosition = new Vector3( 0,0,6 );
		myTextObject.transform.position = tempPosition; 
		
		myTextObject.AddComponent("TextMesh");
		
		myTextObject.AddComponent("MeshRenderer");	
		
		TextMesh textMeshComponent = myTextObject.GetComponent<TextMesh>();
		
		MeshRenderer meshRendererComponent = myTextObject.GetComponent<MeshRenderer>();		
		 
		
		textMeshComponent.font = ArialFont;

		textMeshComponent.text = "im alive";
		
                // i thought this part set the renderer material
		myTextObject.renderer.material.shader=Shader.Find("GUI/Text Shader");
		
	}

When i run it, i see weird blocks instead of text.
I then check out the inspector, I see under Mesh Renderer - Materials no elements have been selected.

If I select a diffrent font in the Text Mesh, the text is shown in the Game view. Further more, under Mesh Renderer - Materials there is now a font material present.

Can anyone point me in the right direction how to set the Font Material in the GameObject’s Mesh Renderer?

Many thanks.

Regards,

Ed.

You need to set the texture for the material used in the mesh renderer, specifically the texture generated from the font. As an aside, AddComponent already returns the component, so you don’t need to use AddComponent and then GetComponent. Also don’t use strings in AddComponent.

var meshRendererComponent = myTextObject.AddComponent<MeshRenderer>();

–Eric

Thanks for pointing me in the right direction. :smile:

I didnt realise ArialFont had the material.

Perhaps i’m the only one who got stuck here but here is my complete code as i couldnt find one complete working example:

void Start () {
			
		Font ArialFont = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");    

		GameObject myTextObject = new GameObject("test");
		Vector3 tempPosition = new Vector3( 0,0,6 );
		myTextObject.transform.position = tempPosition; 
		
		TextMesh textMeshComponent = myTextObject.AddComponent<TextMesh>();
		
		textMeshComponent.font = ArialFont;

		textMeshComponent.text = "im alive";

		myTextObject.renderer.material = ArialFont.material;
		
	}
1 Like