Introduction
The UI shows players what is happening in the game. It helps players understand the game state without guessing.
Jump into concepts and practice - no test required
The UI shows players what is happening in the game. It helps players understand the game state without guessing.
public class GameUI : MonoBehaviour { public Text healthText; public void UpdateHealth(int currentHealth) { healthText.text = "Health: " + currentHealth; } }
healthText.text = "Score: " + playerScore;if (isGameOver) {
gameOverPanel.SetActive(true);
}timerText.text = $"Time Left: {timeRemaining}s";This Unity script updates the UI to show the player's health and score. When health reaches zero, it shows a 'Game Over' panel.
using UnityEngine; using UnityEngine.UI; public class GameUI : MonoBehaviour { public Text healthText; public Text scoreText; public GameObject gameOverPanel; private int playerHealth = 100; private int playerScore = 0; void Start() { UpdateHealth(playerHealth); UpdateScore(playerScore); gameOverPanel.SetActive(false); } public void UpdateHealth(int currentHealth) { healthText.text = "Health: " + currentHealth; if (currentHealth <= 0) { GameOver(); } } public void UpdateScore(int currentScore) { scoreText.text = "Score: " + currentScore; } void GameOver() { gameOverPanel.SetActive(true); } // Example methods to simulate game events public void TakeDamage(int damage) { playerHealth -= damage; UpdateHealth(playerHealth); } public void AddScore(int points) { playerScore += points; UpdateScore(playerScore); } }
Always keep UI updates in sync with the game state to avoid confusing players.
Use clear and simple UI elements to communicate important game information.
Test UI changes to ensure they appear at the right time during gameplay.
UI shows players what is happening in the game.
Update UI elements whenever the game state changes.
Clear UI helps players enjoy and understand the game better.
int health = 50;
Text healthText;
void UpdateHealthUI() {
healthText.text = "Health: " + health;
}
UpdateHealthUI();
What will be shown on the UI if health is 50?int score = 10;
Text scoreText;
void UpdateScore() {
scoreText.text = score;
}