0
votes

Alright, working in Unity 2019.4.1 I have a glitch where, if I change the font of my Text Mesh via script at runtime, the characters get garbled:

enter image description here

I tried a solution from the forums here:

void resetChars(TextMesh textMesh)
    {
        textMesh.text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        textMesh.font.characterInfo = null;
        textMesh.font.RequestCharactersInTexture(textMesh.text, textMesh.fontSize, textMesh.fontStyle);
    }

if(Input.GetKey("space"))
        {
            resetChars(GetComponent<TextMesh>());
            GetComponent<TextMesh>().font = (Font)Resources.Load("Inter-Black-slnt=0");
        }

But this made it worse. What is happening here and how can I change my Text Mesh font dynamically?

2

2 Answers

0
votes

Put your font in a Resources file directly in asset file then use that method style.font = Resources.Load<Font>("Fonts/<fileName");.

https://docs.unity3d.com/2018.1/Documentation/ScriptReference/UI.Text-font.html look here to.

0
votes

The material of the MeshRenderer must be the same as the font's material.