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);
}
}