- Home /
How to make heart health system?
Hi everyone! I am trying to make 3 lives for the player, each time a ball hit him, hearts decrease and disappear till the end of the game. I have two problems:
When the ball hit the player, hearts still remaining no one disappear
I am using LoadLevel, so when some heart disappear it appear again when reloading the level, how to not restore hearts total amount. I tried DontDestroy function, but didn't work for me. Here is few lines of the code
void CheckHealth() {
switch (Health) {
case 3:
h1.gameObject.SetActive (true);
h2.gameObject.SetActive (true);
h3.gameObject.SetActive (true);
break;
case 2:
h1.gameObject.SetActive (true);
h2.gameObject.SetActive (true);
h3.gameObject.SetActive (false);
//Health= PlayerPrefs.SetInt("Health", 2);
break;
case 1:
h1.gameObject.SetActive (true);
h2.gameObject.SetActive (false);
h3.gameObject.SetActive (false);
//Health= PlayerPrefs.SetInt("Health", 1);
break;
case 0:
h1.gameObject.SetActive (false);
h2.gameObject.SetActive (false);
h3.gameObject.SetActive (false);
gameover.gameObject.SetActive (true);
//Health= PlayerPrefs.SetInt("Health", 0);
Time.timeScale = 0;
break;
}
}
void OnTriggerEnter2D(Collider2D col) {
string[] name = col.name.Split ();
if (name.Length > 1) {
if (name [1] == "Ball") {
Health--;
CheckHealth ();
StartCoroutine (KillThePlayer ());
}
}
}
Answer by Meishin · Jun 15, 2019 at 07:54 PM
Hello @m0mohy92,
For your 1st issue ; How are defined h1, h2, h3 ? If those are UI images (attached to an UI canvas) then it should work with "hN.enabled (false);" (remove the ".gameObject")
For the 2nd issue (passing a variable from scene to scene) you can use DontDestroyOnLoad() as you said, tho you need your script to be either a monobehavior derived class or attached to a gameObject ;
public void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
Of course if your script is on the player itself and you destroy him when you exit your scene it's not gonna work (simply put the UI update in another script / on an other empty gameobject)
All in all your script should look like ;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// Attach to an empty GameObject
// To initialize script on a new scene, add updateHealthUI() in the Awake or Start Method of your player
// Then just use this script in your OnCollision method using thisScript.Health --
public class UpdateHealthUI : MonoBehaviour // MonoBehaviour
{
// Insert your 3 hearts images in the Unity Editor
[SerializeField] private Image h1, h2, h3;
// Create an array because we're lazy
private Image[] images;
// Gameover
[SerializeField] private Image gameOver;
// A private variable to keep between scenes
[SerializeField] private int health = 3;
// Now we define Get / Set methods for health
// In case we Set health to a different value we want to update UI
public int Health { get { return health; } set { if (health != Health) health = Health; updateHealthUI(); } }
public void Awake()
{
DontDestroyOnLoad(this.gameObject);
images = new Image[] { h1, h2, h3 };
}
private void updateHealthUI()
{
for(int i = 0; i < images.Length;i++)
{
// Hide all images superior to the newHealth
if (i >= health)
images[i].enabled = false;
else
images[i].enabled = true;
}
// Game Over
if (health == 0)
{
gameOver.enabled = true; ;
Time.timeScale = 0;
}
}
}
Answer by EDevJogos · Jun 15, 2019 at 08:05 PM
You can use void OnSceneLoaded(Scene scene, LoadSceneMode mode)
to check when a level is loaded and then call your method CheckHealth();
inside. Note that Health would have to be declared as static
so that it's value persist during change of scenes.
As for why the hearts are not disapearing when a ball hits the player i don't know, is hard to say by just looking at the piece of code you provided, the best i can say to you is to Debug, see if the object name is actually what you expect and things like that.
For More info on the SceneLoaded callback check: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html
Answer by Constantin_d · Jun 10, 2020 at 11:29 AM
This is a really easy way to do it. If you like, check it out.
https://www.youtube.com/watch?v=Dg-6qoZ2JqI&list=PLyz_wKnxtoQO0Tf3koreC4gJtFv4213yQ
Your answer
Follow this Question
Related Questions
How to make player keep getting hurt when touching fire? 1 Answer
Player health dropping far to fast 1 Answer
Spawing small sprites proportionate to health 0 Answers
how do u make diffrent chacters and give them commands 2 do things 2 Answers
how to rightly detect a punch and manage health in a 3d fight/combat type game? 0 Answers