How to Create Text in Unity: Simple Steps for Beginners
To create text in Unity, add a
Text component from the UI menu or use TextMeshPro for better quality. You can then set the text content, font, size, and color in the Inspector or via script.Syntax
Unity provides two main ways to create text: UI Text and TextMeshPro. Both are components you add to a GameObject.
Text: A simple UI text component for 2D interfaces.TextMeshProUGUI: A more advanced text component with better rendering and styling.
You can set the text by assigning a string to the text property.
csharp
using UnityEngine; using UnityEngine.UI; public class TextExample : MonoBehaviour { public Text uiText; void Start() { uiText.text = "Hello, Unity!"; } }
Example
This example shows how to create a UI Text object in Unity and set its text from a script.
csharp
using UnityEngine; using UnityEngine.UI; public class CreateText : MonoBehaviour { void Start() { // Create Canvas GameObject canvasGO = new GameObject("Canvas"); Canvas canvas = canvasGO.AddComponent<Canvas>(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; canvasGO.AddComponent<CanvasScaler>(); canvasGO.AddComponent<GraphicRaycaster>(); // Create Text GameObject GameObject textGO = new GameObject("MyText"); textGO.transform.SetParent(canvasGO.transform, false); // Add Text component Text text = textGO.AddComponent<Text>(); text.text = "Hello, Unity!"; text.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text.fontSize = 30; text.color = Color.black; // Position and size RectTransform rectTransform = text.GetComponent<RectTransform>(); rectTransform.localPosition = Vector3.zero; rectTransform.sizeDelta = new Vector2(400, 100); } }
Output
A black text 'Hello, Unity!' appears centered on the screen inside a Canvas.
Common Pitfalls
Common mistakes when creating text in Unity include:
- Not adding a
Canvasbefore adding UI Text, so the text won't show. - Forgetting to assign a font to the
Textcomponent, resulting in invisible text. - Using
Textinstead ofTextMeshProwhen better text quality is needed. - Not setting the
RectTransformsize and position, causing text to be off-screen or clipped.
csharp
/* Wrong way: No Canvas, no font assigned */ GameObject textGO = new GameObject("Text"); Text text = textGO.AddComponent<Text>(); text.text = "Invisible text"; // Won't show /* Right way: Add Canvas and assign font */ GameObject canvasGO = new GameObject("Canvas"); Canvas canvas = canvasGO.AddComponent<Canvas>(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; textGO.transform.SetParent(canvasGO.transform, false); text.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text.text = "Visible text";
Quick Reference
Summary tips for creating text in Unity:
- Always create or use a
Canvasfor UI text. - Assign a font to your
Textcomponent to make text visible. - Use
TextMeshProfor sharper and more customizable text. - Adjust
RectTransformto position and size your text properly.
Key Takeaways
Add a Canvas before creating UI Text to ensure it displays.
Assign a font like Arial to your Text component to make text visible.
Use TextMeshPro for better text quality and styling options.
Set RectTransform size and position to control where text appears.
You can change text content anytime by setting the text property in code.