-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHealthUI.cs
More file actions
49 lines (42 loc) · 1.2 KB
/
HealthUI.cs
File metadata and controls
49 lines (42 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthUI : MonoBehaviour
{
public int maxHealth = 3; // How many hearts total
public int currentHealth; // Current health value
public GameObject heartPrefab; // The heart UI prefab
public Transform heartsParent; // Where the hearts will be placed in the Canvas
private List<GameObject> hearts = new List<GameObject>();
void Start()
{
currentHealth = maxHealth;
DrawHearts();
}
void DrawHearts()
{
// Clear old hearts
foreach (GameObject heart in hearts)
{
Destroy(heart);
}
hearts.Clear();
// Draw hearts based on current health
for (int i = 0; i < currentHealth; i++)
{
GameObject newHeart = Instantiate(heartPrefab, heartsParent);
hearts.Add(newHeart);
}
}
public void TakeDamage(int amount)
{
currentHealth -= amount;
if (currentHealth < 0) currentHealth = 0;
DrawHearts();
if (currentHealth <= 0)
{
Debug.Log("Game Over!");
// Call your game over logic here
}
}
}