aboutsummaryrefslogtreecommitdiff
blob: 685e297053bd92f30736873153d8b8b6f056b267 (plain)
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
50
51
52
53
54
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HealthBar : MonoBehaviour
{
    public GameObject crystalPrefab;
	public Material liveCrystal;
	public Material deadCrystal;
	
	public int lives;
	
	private int currentHealth;
	public int health
	{
		get{return currentHealth;}
		set
		{
			currentHealth = value;
			updateCrystals();
		}
	}
	
	private List<Renderer> crystals = new List<Renderer>();
	
	private void updateCrystals()
	{
		for(int i=0; i<lives; i++)
		{
			if(health > i)
				crystals[i].material = liveCrystal;
			else
				crystals[i].material = deadCrystal;
		}
	}
	
    void Start()
    {
        for(int i=0; i<lives; i++)
		{
			GameObject crystal = Instantiate(crystalPrefab, transform.position + Vector3.right*1.4f*i, Quaternion.identity);
			crystals.Add(crystal.GetComponentInChildren<Renderer>());
		}
		
		health = lives;
		updateCrystals();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}