Quantcast
Channel: Questions in topic: "count"
Viewing all articles
Browse latest Browse all 187

How can i decrease lives?

$
0
0
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(); 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; } } }

Viewing all articles
Browse latest Browse all 187

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>