Question by
israhm70 · Jul 01, 2021 at 04:16 PM ·
playerprefshealthbar
MaxHealth not updating after scene changes.
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 halfHeart;
public Sprite emptyHeart;
private GameManager theGM;
private void Start()
{
theGM = FindObjectOfType<GameManager>();
health = PlayerPrefs.GetInt("CurrentLives");
}
private 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;
}
}
if (health == 0)
{
theGM.GameOver();
health = numOfHearts;
}
}
public void TakeLife()
{
health--;
PlayerPrefs.SetInt("CurrentLives", health);
}
public void AddLife()
{
health++;
PlayerPrefs.SetInt("CurrentLives", health);
}
public void AddMaxHealth(int increase)
{
numOfHearts += increase;
health = numOfHearts;
PlayerPrefs.SetInt("CurrentLives", health);
}
}
This is my health system. I created an object that on trigger increases max health and everything is working but when I change scene, current health stays alright but the extra health doesn't appear (Doesn't update) Any help? Thanks in advance!
Comment
Your answer
Follow this Question
Related Questions
PlayerPrefs defaulting to 0 4 Answers
Unity builds with priop playerprefs 0 Answers
Saving an image's state 2 Answers
How to save SetActive states 0 Answers
Help making a high score with player prefs/displaying it 1 Answer