Text and TextMeshPro let you show words on the screen in your game or app. They help make your messages clear and nice to look at.
Text and TextMeshPro in Unity
Start learning this pattern below
Jump into concepts and practice - no test required
using UnityEngine; using TMPro; public class ShowText : MonoBehaviour { public Text uiText; // For UI Text component public TextMeshProUGUI tmpText; // For TextMeshPro UI component void Start() { uiText.text = "Hello World!"; tmpText.text = "Hello TextMeshPro!"; } }
Text is the older Unity UI text component, simpler but less flexible.
TextMeshPro is newer, sharper, and supports more styling options.
uiText.text = "Score: 100";tmpText.text = "Game Over!";tmpText.color = Color.red;
uiText.fontSize = 24;This script sets a TextMeshProUGUI text to say "Welcome to the game!" in green color and large font size when the game starts.
using UnityEngine; using TMPro; public class TextExample : MonoBehaviour { public TextMeshProUGUI messageText; void Start() { messageText.text = "Welcome to the game!"; messageText.color = Color.green; messageText.fontSize = 36; } }
TextMeshPro uses signed distance field rendering for sharp text at any size.
Always use TextMeshPro for better quality and more features in modern Unity projects.
Remember to add the TextMeshPro package via Unity Package Manager if not already included.
Text and TextMeshPro display text on screen in Unity.
TextMeshPro offers better quality and styling than the older Text component.
Use TextMeshPro for modern, clear, and flexible text in your games or apps.
Practice
Solution
Step 1: Understand Unity text components
Unity has a basic Text component and a more advanced TextMeshPro component for displaying text.Step 2: Identify which is sharper and customizable
TextMeshPro is designed to provide sharper, clearer text with more customization options than the basic Text component.Final Answer:
TextMeshPro -> Option CQuick Check:
Sharper text = TextMeshPro [OK]
- Confusing Canvas Renderer with text rendering
- Thinking Sprite Renderer handles text
- Choosing Audio Source which is unrelated
Solution
Step 1: Recall TextMeshPro text property usage
TextMeshPro components have a public property called 'text' to set the displayed string.Step 2: Identify correct syntax
Assigning a string directly to 'text' like myText.text = "Hello World!"; is correct and common.Final Answer:
myText.text = "Hello World!"; -> Option DQuick Check:
Set text with .text property [OK]
- Using SetText() which exists but is a method, not property assignment
- Using non-existent properties like content or display
- Missing quotes around string
myText is a TextMeshProUGUI component?
myText.text = "Score: " + 10 + 5;
Solution
Step 1: Understand string concatenation in C#
When using + with a string and numbers, numbers are converted to strings and concatenated left to right.Step 2: Evaluate expression "Score: " + 10 + 5
"Score: " + 10 becomes "Score: 10", then + 5 becomes "Score: 105" as 5 is appended as string.Final Answer:
Score: 105 -> Option BQuick Check:
String + numbers concatenates left to right [OK]
- Assuming 10 + 5 adds first before string concatenation
- Expecting spaces between numbers automatically
- Thinking + signs appear in output
public TextMeshProUGUI myText;
void Start() {
myText.text = "Game Over";
}
What is the most likely reason?Solution
Step 1: Check common setup mistakes
If myText is not assigned in the Inspector, the script has a null reference and text won't update.Step 2: Verify other options
TextMeshProUGUI has a text property; no Refresh() needed; setting text in Start() works fine.Final Answer:
The myText variable is not assigned in the Inspector -> Option AQuick Check:
Assign UI references in Inspector [OK]
- Assuming text property doesn't exist
- Thinking Refresh() is required
- Believing Start() is too early to set text
playerHealth?Solution
Step 1: Review ways to convert int to string in C#
playerHealth is an int; to display it, convert to string or use string interpolation or formatting.Step 2: Check each option
B uses + and ToString(), C uses string.Format(), D uses string interpolation with $"..."; all work.Step 3: Confirm all are correct
All three methods correctly update the text with the health value.Final Answer:
All of the above -> Option AQuick Check:
Multiple string methods work for text update [OK]
- Forgetting to convert int to string
- Using incorrect string syntax
- Updating text outside Update() when dynamic
