using UnityEngine; using UnityEngine.UI; public class GameStateUI : MonoBehaviour { public int playerHealth = 10; public Text statusText; void Update() { if (playerHealth <= 0) { statusText.text = "Game Over"; } else { statusText.text = "Health: " + playerHealth; } } }
The code updates the UI text every frame. When playerHealth is 0 or less, it sets the text to "Game Over". Otherwise, it shows the current health.
The UI shows important information like health, score, or status. This helps players understand what is happening and decide what to do next.
using UnityEngine; using UnityEngine.UI; public class GameStateUI : MonoBehaviour { public int playerHealth = 10; public Text statusText; void Start() { if (playerHealth <= 0) { statusText.text = "Game Over"; } else { statusText.text = "Health: " + playerHealth; } } }
The UI text is set only once in Start(). If playerHealth changes later, the UI does not update because Update() or another method is not changing the text.
using UnityEngine; using UnityEngine.UI; public class GameStateUI : MonoBehaviour { public int playerHealth = 10; public Text statusText; void Update() { if (playerHealth <= 0) { statusText.text = "Game Over"; } } }
The line 'public Text statusText' is missing a semicolon at the end, causing a compile error.
Using separate UI elements for each piece of information helps players quickly understand their status without confusion.
