Hey Guys I want my player to lose one Heartcontainer if he collides with the enemy
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Health : MonoBehaviour { public int health; public int numOfHearts;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
public void damage(int n) //n represents the health number the player is going to lose
{
//TODO
}
public void ReFill(int n) //n represents the health number the player is going to gain
{
//TODO
}
public Collider player_co;
private void Start()
{
player_co = GetComponent<Collider>();
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag.Equals("Enemy"))
{
damage(1);
}
}
void Update()
{
if (health>numOfHearts) {
health = numOfHearts;
}
for (int i=0;i< hearts.Length; i++) {
if(i < health)
{
hearts[i].sprite = fullHeart;
}
else { hearts[i].sprite = emptyHeart; }
if (i < numOfHearts)
{
hearts[i].enabled = true;
}
else {
hearts[i].enabled = false;
}
}
}
},
Comment