0
0
Unityframework~10 mins

Text and TextMeshPro in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Text and TextMeshPro
Create UI Canvas
Add Text or TextMeshPro Component
Set Text Content
Customize Font, Size, Color
Run Scene
Text Displays on Screen
This flow shows how to add and display text using Unity's Text or TextMeshPro components step-by-step.
Execution Sample
Unity
using TMPro;
using UnityEngine;

public class DisplayText : MonoBehaviour {
    public TextMeshProUGUI tmpText;
    void Start() {
        tmpText.text = "Hello, Unity!";
    }
}
This code sets the text of a TextMeshProUGUI component to display a message when the scene starts.
Execution Table
StepActionComponent StateOutput on Screen
1Scene starts, Start() calledtmpText.text = ""No text shown yet
2Set tmpText.text = "Hello, Unity!"tmpText.text = "Hello, Unity!"Text 'Hello, Unity!' appears on screen
3Scene runningtmpText.text = "Hello, Unity!"Text remains visible
💡 Text is set once at Start and remains displayed until changed or scene ends
Variable Tracker
VariableStartAfter Step 2Final
tmpText.text"""Hello, Unity!""Hello, Unity!"
Key Moments - 2 Insights
Why does the text not appear if tmpText is not assigned in the inspector?
If tmpText is null (not linked), setting tmpText.text throws a NullReferenceException and no text appears. See execution_table step 2 where tmpText.text changes only if tmpText is assigned.
What is the difference between Text and TextMeshPro components?
TextMeshPro uses better text rendering with more options like rich text and better clarity, while Text is simpler but less flexible. Both follow the same flow but TextMeshPro is recommended.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is tmpText.text at Step 1?
A"Hello, Unity!"
B"" (empty string)
Cnull
D"Start"
💡 Hint
Check the 'Component State' column at Step 1 in the execution_table
At which step does the text appear on the screen?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
Look at the 'Output on Screen' column in the execution_table
If tmpText is not assigned in the inspector, what happens when Start() runs?
AText shows as empty
BText shows 'Hello, Unity!'
CNullReferenceException error
DNo text appears and no error
💡 Hint
Consider what happens if tmpText is null when setting tmpText.text in the code
Concept Snapshot
Unity Text and TextMeshPro:
- Add Text or TextMeshProUGUI component to Canvas
- Assign component to script variable
- Set .text property to change displayed text
- TextMeshPro offers better quality and features
- Text updates appear immediately in UI
Full Transcript
This lesson shows how to display text in Unity using Text and TextMeshPro components. First, you create a Canvas and add a Text or TextMeshProUGUI component. Then, you assign that component to a script variable. In the script's Start method, you set the .text property to the message you want to show. When the scene runs, the text appears on the screen. If the component is not assigned, setting .text causes an error. TextMeshPro is recommended for better text quality and options.