Post tutorial RSS How to Display text in Unity UI

Making a UI is an integral part of game development. In this tutorial, we will see how to display a basic text element in Unity and change its content using script.

Posted by on - Basic UI/HUD

I am using Unity 2021.3.2 for this tutorial

Open a new Project in Unity. Let's add a text element by going to the Hierarchy window>clicking on the + sign> UI> Legacy>Text. In the older versions of Unity, the text element will be under UI.

You can select the text element and change the properties like font, size, alignment and more in the inspector window.

Text Properties

If you want to change the content of the text during game. For example, if you want to display a timer then the content inside the text is dynamic. Let take the Unity timer script from VionixStudio and try to display it in our text element

Here is the code

Create a new empty game object. Click on the + sign>Empty game object in the hierarchy window. Select the game object and click on add component. Select newscript. Name the script Timerexample and click on create and add.

Open the script for editing and paste the code from above.

The code has a reference to the public variable text. Let's assign our text element to the game object with the code.

Assign text

Now play the game and the timer value will be displayed in the text UI.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.