How to write a Text Mesh Pro text from Script in Unity

Introduction

In this arti­cle we will see how to work with the Text Mesh Pro com­po­nents from a Script, in addi­tion you will find a video from the chan­nel in which we will cre­ate a TEXT OBJECT to dis­play in a Can­vas and anoth­er Text Mesh for the 3D space and we will cre­ate a Script inside which we will mod­i­fy the text that these com­po­nents show, as an extra we will also mod­i­fy the col­or by code.

Text Mesh Pro has now become the stan­dard solu­tion for dis­play­ing text in Uni­ty, replac­ing the Text Mesh com­po­nent for dis­play­ing text in the 3D view and the Text com­po­nent for adding text in the UI. The old Text Mesh and Text com­po­nents can still be used, only that they are in a "Lega­cy" sec­tion, this is indi­cat­ing that it is prefer­able to use Text Mesh Pro and per­haps lat­er the old com­po­nents will no longer be available.

Fig.1: The Text com­po­nent has been moved to the "Lega­cy" section.

Related video: Use Text Mesh PRO from script in Unity

In this video we see how to use Text Mesh PRO in Uni­ty in both, world space and screen space, start­ing by import­ing the tools, add a text to the screen and then we see how to write that Text Mesh Pro text in Uni­ty through code. The video is divid­ed in chap­ters so you can watch a spe­cif­ic part if you need.


Creating Text Mesh Pro objects for World Space and Canvas

We start by cre­at­ing the Text objects that we will lat­er mod­i­fy from a Script, we are going to cre­ate two types of Text Mesh Pro objects, one to use in the user inter­face and anoth­er to use as a 3D object in the scene.

Creating Text Mesh Pro Text for the user interface

In Uni­ty the Text Mesh Pro objects that are in the UI sec­tion must be placed as chil­dren of a Can­vas object, so let's assume that we already have one of these objects in the scene. To cre­ate a new Text Mesh Pro object we go to the hier­ar­chy, right click on the Can­vas (or any child object of the Can­vas), go to the UI sec­tion and choose the "Text — Text Mesh Pro" option, as shown in fig­ure 2.A.

Fig. 2.A: Option to cre­ate a new Text Mesh Pro text for the user interface.

Creation of Text Mesh Pro Text for World Space

The oth­er option to write text on the screen is to use a Text com­po­nent of Text Mesh Pro as a 3D object and there­fore locat­ed in a posi­tion in the world, this object will be found in the "3D Object" sec­tion of the cre­ation win­dow, as shown in fig­ure 2.B.

Fig. 2.B: Option to cre­ate a new Text Mesh Pro text as a 3D object in the scene.

First time using Text Mesh Pro

In case we have not con­fig­ured Text Mesh Pro yet, we will get the win­dow shown in fig­ure 3 where we will be giv­en the option to import the nec­es­sary com­po­nents to use Text Mesh Pro, we click on "Import TMP Essen­tials", as shown in fig­ure 3. The sec­ond but­ton to import exam­ples and extras is optional.

Fig­ure 3: Win­dow for import­ing Text Mesh Pro pack­age into Unity.

Result of the creation of objects

Once the objects were cre­at­ed, I made a few mod­i­fi­ca­tions in the inspec­tor (font size, text) and the result is as follows:

Fig. 4.a: Text Mesh Pro objects in the hierarchy.
Fig. 4.b: Text Mesh Pro objects dis­played in the scene.

Once the objects have been cre­at­ed and Text Mesh Pro import­ed we can start using the Text Mesh Pro Text com­po­nent from the inspec­tor or write it through a Script. In fig­ure 5 we see the Text com­po­nent in the Inspec­tor win­dow, it has many more con­fig­u­ra­tion options com­pared to the old text solution.

IMPORTANT

In fig­ure 5 we see the field to edit the text that appears on the screen, cur­rent­ly has writ­ten the val­ue "Can­vas Text", that is the field that we want to edit by code and to do it we will have to edit a vari­able called "text" that is define in that component.

Fig. 5: Text Mesh Pro com­po­nent in the inspector.

Script for writing text in Text Mesh Pro component

In order to write a Text Mesh Pro com­po­nent by code I will cre­ate a script and assign it to some GameOb­ject of the hier­ar­chy, as shown in fig­ure 6. In this case my script is called "Mod­i­fy­TextMesh­Pro", inside this script I will mod­i­fy the texts.

Fig. 6: We cre­ate a script and assign it to some object in the hierarchy.

Import TMPro namespace in our Script

To be able to use the Text Mesh Pro com­po­nents com­fort­ably, it is con­ve­nient to import the "TMPro" name­space by adding in the head­er of our script the line "using TMPro;" as shown in fig­ure 7.

Fig. 7: We declare that we are going to use the name­space "TMPro" in the head­er of our script.

Declaration of the variables to be used

We are going to declare two vari­ables of type "TMP_Text" where the ref­er­ences of the Text com­po­nents that we want to mod­i­fy will be stored, in this case the names of my vari­ables will be "can­vas­Text" and "world­Text", in these vari­ables I will place the Text Mesh Pro Text com­po­nents of the can­vas and the world space respectively.

DETAIL

The names "can­vas­Text" and "world­Text" are the names I chose for these vari­ables, you can use any oth­er name as long as it con­tains the allowed characters.

Fig. 8: Dec­la­ra­tion of the vari­ables to be used to mod­i­fy the Text Mesh Pro text.

Initialization of variables (Assignment of references)

The ini­tial­iza­tion of this type of non-prim­i­tive vari­ables is cru­cial, if we do not take care of putting inside the vari­able the pre­cise object we want to refer to, we will get a null ref­er­ence exception.

There are many ways to ini­tial­ize the vari­ables, in this case I will do it in one of the sim­plest ways which is by drag­ging the GameOb­jects that con­tain the Text com­po­nents I want to mod­i­fy to the vari­able spaces in the inspector.

Fig. 9: The appro­pri­ate GameOb­jects are dragged into the spaces in the inspec­tor to ini­tial­ize the variables.

The declared variable does not appear in the inspector

In the case that the vari­able does not appear in the inspec­tor it is usu­al­ly because its vis­i­bil­i­ty is pri­vate, it can be solved by declar­ing the vari­ables as pub­lic as shown in fig­ure 8, adding the word "pub­lic", or they can also be declared as pri­vate but indi­cat­ing that they are seri­al­ized by the inspec­tor, as follows:

[Seri­al­ize­Field]
TMP_Text can­vas­Text;

Or:

[Seri­al­ize­Field]
pri­vate TMP_Text canvasText;

Anoth­er rea­son why the vari­ables do not appear in the inspec­tor can be when there are errors in con­sole and the changes made in the scripts are not updat­ed, to solve this we will have to solve all the errors that there are in con­sole, once made this Uni­ty will com­pile and the new mod­i­fi­ca­tions will appear.

Code instructions for modifying Text Mesh Pro text via Script and tests

Once we have ini­tial­ized the vari­ables we can use them, in this case if we want to mod­i­fy the text dis­played by the Text Mesh Pro com­po­nent, we must mod­i­fy the vari­able "text" defined inside it, for this we use the dot oper­a­tor that allows us to access the vari­ables and pub­lic func­tions defined inside an object,

Fig. 10.A: Writ­ing a Text Mesh Pro text from a Script in Unity.
Fig. 10.B: Press­ing play shows how the texts on the screen are modified.

Extra: Change the color of a Text Mesh Pro text by code

Fig. 11.A: In lines 22 and 23 the col­or of the Text Mesh Pro texts is changed by code.
Fig. 11.B: When press­ing play we can see how the col­ors of the texts on the screen change.

Scroll to Top
Secured By miniOrange