How can i decrease lives?
Hello, i'm currently making a 2D platform game. So far i have a script for character movement, item collection and enemy movement. In terms of interface, i have a system of three lives with heart icons. I have added a void Die() for states like falling off the screen. However, when the main character falls, he loses all the lives at once. ¿So, how can i make it lose only one life each time he falls?
Here is the script i have made, related with this issue:
private Rigidbody2D rb;
private GameObject PlayerObject;
public int health;
public int numOfHearts;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
void Start()
{
rb = GetComponent<Rigidbody2D>();
PlayerObject = GameObject.Find("Player");
PlayerPrefs.GetInt("Lives", 3);
numOfHearts = PlayerPrefs.GetInt("Lives", 3);
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Enemy")
{
anim.Play("Hurting");
Destroy(this.gameObject, 1f);
Die();
}
}
void Die()
{
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;
}
}
}
Your answer
Follow this Question
Related Questions
How can I slow down just my character when holding down on the Screen 1 Answer
Shooting an instantiated missile the way a object is facing. 0 Answers
How do I make a scrollbar for the camera? 0 Answers
How to add more UI elements to Dropdown OptionData except default string and sprite values in Unity? 0 Answers
Make Jump Fish game 1 Answer