Skip to main content
Unity 3D Basics – Complete Beginner to Advanced Guide
CHAPTER 08 Beginner

Unity UI System

Updated: May 16, 2026
25 min read

# CHAPTER 8

Unity UI System

1. Introduction

No matter how great your gameplay is, if the player doesn't know how much health they have, what their score is, or how to start the game, the experience fails. User Interface (UI) is the bridge between the game's hidden mathematics and the player's brain. In this chapter, we will master the Unity UI System. We will explore the Canvas, use TextMeshPro for crisp typography, create interactive Buttons, learn how to anchor elements to different screen sizes, and write C# code to update the UI dynamically during gameplay.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the 2D Canvas and how it overlays the 3D world.
  • Create and manipulate TextMeshPro (TMP) elements.
  • Configure UI Anchors for responsive design across different resolutions.
  • Create interactive UI Buttons.
  • Link C# scripts to the UI to update scores and health dynamically.

3. The Canvas

In Unity, UI elements do not exist in standard 3D space. They live on a Canvas.
  • Right-click the Hierarchy -> UI -> Canvas.
  • The Canvas is essentially a massive invisible 2D movie screen projected directly over the player's camera.
  • Rule: Every single UI element (Text, Image, Button) *must* be a child of a Canvas. If you drag text outside the Canvas, it will disappear.

4. UI Anchors (Responsive Design)

A PC monitor is widescreen (16:9). An iPad is boxy (4:3). A mobile phone is vertical. How do you ensure your "Score" text stays in the top-right corner on every device? Anchors.
  • Click your UI Text. In the Inspector, click the square Rect Transform anchor icon.
  • Hold Shift + Alt and click the Top-Right option.
  • This pins the text to the top-right corner of the screen, regardless of the monitor's resolution or aspect ratio!

5. TextMeshPro (TMP)

Unity's old text system was blurry and pixelated. The modern standard is TextMeshPro.
  • Right-click Canvas -> UI -> Text - TextMeshPro. (Unity may ask you to "Import TMP Essentials"—always click yes).
  • TMP uses advanced rendering to ensure your text is perfectly sharp whether it is size 10 or size 500.
  • You can change the Font, Color, Outline, and Shadow directly in the Inspector.

6. Interactive Buttons

A Main Menu needs buttons.
  • Right-click Canvas -> UI -> Button - TextMeshPro.
  • A Button is actually an Image with a script attached, plus a child Text object.
  • In the Inspector, the Button component has an On Click () list. This is where you tell Unity exactly which C# function to run when the player clicks the button with their mouse.

7. Updating UI with C# Scripts

To make a score go up, your C# script needs to talk to the TextMeshPro component. You must add using TMPro; at the top of your script.
csharp
1234567891011121314
using UnityEngine;
using TMPro; // REQUIRED for TextMeshPro!

public class ScoreManager : MonoBehaviour
{
    public TextMeshProUGUI scoreText; // Drag the UI Text object here in the Inspector!
    private int score = 0;

    public void AddScore()
    {
        score += 10;
        scoreText.text = "Score: " + score; // Update the UI visually
    }
}

8. Visual Learning: The UI Hierarchy

txt
12345
[ Canvas ] (The 2D Screen Overlay)
   |-- [ Text (TMP) ] "Health: 100" (Anchored Top-Left)
   |-- [ Text (TMP) ] "Score: 0"    (Anchored Top-Right)
   |-- [ Button ]                   (Anchored Center)
         |-- [ Text (TMP) ] "Start Game"

9. Best Practices

  • Canvas Scaler: Click your Canvas object. In the Inspector, find the Canvas Scaler component. Change the "UI Scale Mode" from Constant Pixel Size to Scale With Screen Size. Set the Reference Resolution to 1920 x 1080. This ensures your UI scales up perfectly on a 4K TV, and scales down perfectly on a 720p mobile phone without breaking your layout.

10. Common Mistakes

  • Clicking the 2D Button: Editing a 2D UI inside a 3D perspective Scene View is maddening. When editing the Canvas, look at the very top of the Scene View window and click the small "2D" button. This flattens the camera, making it vastly easier to drag and align UI elements.

11. Mini Project: Build a Clicker Game

Objective: Create a button that increases a score counter on the screen.
  1. 1. Create a Canvas. (Set it to Scale with Screen Size).
  1. 2. Add a TextMeshPro object. Name it "ScoreText". Anchor it to the Top Center. Change the text to "Score: 0".
  1. 3. Add a Button (TMP). Place it in the middle of the screen. Change its child text to "Click Me!".
  1. 4. Create an empty GameObject in the Hierarchy named GameManager.
  1. 5. Create a script named ClickerGame and attach it to the GameManager.
csharp
123456789101112131415
using UnityEngine;
using TMPro;

public class ClickerGame : MonoBehaviour
{
    public TextMeshProUGUI scoreUI;
    int score = 0;

    // This MUST be public so the Button can see it!
    public void OnButtonClick()
    {
        score++;
        scoreUI.text = "Score: " + score;
    }
}
  1. 6. Click the GameManager, and drag the "ScoreText" UI object into the empty scoreUI slot.
  1. 7. Click the Button UI object. In the Inspector, scroll down to On Click (). Click the + icon.
  1. 8. Drag the GameManager object into the empty slot.
  1. 9. Click the dropdown that says No Function, go to ClickerGame, and select OnButtonClick().
  1. 10. Press Play and click the button!

12. Practice Exercises

  1. 1. What specific using namespace must be added to the top of your C# script to manipulate modern Unity text?
  1. 2. What feature of the RectTransform ensures that a UI element stays in the correct corner of the screen regardless of the monitor's aspect ratio?

13. MCQs with Answers

Question 1

Every piece of UI (Buttons, Images, Text) MUST be a child of which specific Unity component to be rendered on the screen?

Question 2

When writing a custom C# function that you want to be triggered by a UI Button click, what access modifier MUST the function have?

14. Interview Questions

  • Q: Explain the purpose of the Canvas Scaler component. Why is switching from "Constant Pixel Size" to "Scale With Screen Size" mandatory for multi-platform deployment?
  • Q: Walk me through the exact workflow of connecting a visual UI Button to a C# script function. How do you pass the reference in the Unity Editor?
  • Q: Why did Unity deprecate the legacy Text component in favor of TextMeshPro? What underlying rendering technology makes TMP superior?

15. FAQs

Q: Can I put UI elements physically in the 3D world (like a health bar hovering over an enemy's head)? A: Yes! By default, the Canvas is set to "Screen Space - Overlay" (glued to the camera lens). If you change the Canvas Render Mode to "World Space", it becomes a physical 3D object that you can shrink down and place anywhere in the 3D level.

16. Summary

In Chapter 8, we learned how to communicate with the player. We discovered that the Canvas acts as a transparent overlay for all our 2D elements. We utilized RectTransform Anchors and the Canvas Scaler to ensure our UI remains beautiful and responsive on any device. We created crisp, dynamic typography using TextMeshPro, and learned the exact workflow for wiring a clickable UI Button directly into our C# game logic.

17. Next Chapter Recommendation

Our game logic and UI are working, but our player is just a sliding gray cube. It's time for motion. Proceed to Chapter 9: Animations and Animator Controller.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·