void Start() { bar = new HeartBar(30, 4); takeDamageButton.onClick.AddListener(() => bar.TakeDamage(10)); HealButton.onClick.AddListener(() => bar.Heal(5)); int i = 0; foreach (var h in bar.list) { GameObject clone = Instantiate(heartPrefab, heartBarHolder.transform); RectTransform rect = clone.GetComponent <RectTransform>(); rect.localPosition = new Vector3(i * 60, 0, 0); h.foregroundImage = clone.transform.Find("Background/Foreground").GetComponent <Image>(); h.foregroundImage.fillAmount = (float)h.currentHealth / h.maxHealth; i++; } }
private void OnTriggerEnter2D(Collider2D col) { heartBar.TakeDamage(); }