Home » Unity UI Text

Unity UI Text

The UI text element displays a non-interactive piece of text to the user. Text elements can be used to provide captions or labels for other GUI controls or to display instructions or other text. For example printing the current score of the player to the screen requires the numeric value of the score to be converted to a string, generally through the .toString() method, before it is displayed.

To insert a Text UI element in Unity, right-click on the Scene Hierarchy, then select GameObject -> UI -> Text.

Unity UI Text

There are many properties of the Text element. In which Text Field is the most important property. You can type out what you want the text box to show in that field.

Unity UI Text

You can change the font of the text; you must first import the font file from your system into Unity as an Asset.

We can also accept the Text element through the scripting; this is where the importance of dynamic UI comes in.

Let’s see one simple example, instead of the console, which is printing how many times the button has been pressed, as in the previous chapter; let’s print it out on the game screen trough the Text element. To do so, open your previous script file (ButtonAction.cs) and make some changes to it:

Here, we added a public Text variable, where we can drag and drop our Text UI element.

Save your script and go to the ButtonGameObject, and you will now see the new slot for the Text UI element. Drag and drop your Text GameObject on to the slot.

Unity UI Text

Now hit the play button.

Unity UI Text


Next Topic#

You may also like