- Home /
My integer for my player health seems to be only returned once
I can't get my health sprite to change when my player health is at a certain level. I am really not sure how to fix it or explain what is going on. When I run my game, it shows on the console that my health is full. However when I take one damage it doesn't show that my health has lowered.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class HealthSystem
{
public int health;
public int maxHealth;
public Image hpImage;
private Sprite hp;
public HealthSystem(int maxHealth)
{
this.maxHealth = maxHealth;
health = maxHealth;
}
public int GetHealth()
{
return health;
}
public void Damage(int damageAmount)
{
health -= damageAmount;
if (health < 0)
{
health = 0;
}
}
public void Heal(int healAmount)
{
health += healAmount;
if (health > maxHealth)
{
health = maxHealth;
}
}
}
______________________________________________________________________________________________________
public class Player_UI : MonoBehaviour
{
private bool plusOneDmg;
private bool healOneHeart;
private bool padHit = false;
public Image healthImg;
HealthSystem healthSystem = new HealthSystem(4);
private void Start()
{
Debug.Log("Health:" + healthSystem.GetHealth());
}
private void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Lava" && padHit == false)
{
healthSystem.Damage(1);
Debug.Log("Health:" + healthSystem.GetHealth());
padHit = true;
}
else if (collision.gameObject.tag == "HealCube" && padHit == false)
{
healthSystem.Heal(1);
Debug.Log("Health:" + healthSystem.GetHealth());
padHit = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Lava" && padHit == true)
{
padHit = false;
}
else if (collision.gameObject.tag == "HealCube" && padHit == true)
{
padHit = false;
}
}
}
[RequireComponent(typeof(Image))]
public class HealthBar : MonoBehaviour
{
public Sprite Hp4, Hp3, Hp2, Hp1, Hp0;
public Image image;
HealthSystem healthSystem = new HealthSystem(4);
private void Start()
{
hpBar();
image = gameObject.GetComponent<Image>();
}
private void Update()
{
}
private void hpBar()
{
if (healthSystem.health == healthSystem.maxHealth)
{
Debug.Log("FullHp");
image.sprite = Hp4;
}
if (healthSystem.health == 3)
{
Debug.Log("LessHp");
image.sprite = Hp3;
}
if (healthSystem.health == 2)
{
Debug.Log("LesserHp");
image.sprite = Hp2;
}
if (healthSystem.health == 1)
{
Debug.Log("EvenLesserHp");
image.sprite = Hp1;
}
if (healthSystem.health == 0)
{
Debug.Log("NoHp");
image.sprite = Hp0;
}
}
}
Answer by jackmw94 · Jan 18, 2021 at 06:00 PM
Okay, I think this issue is a couple of issues:
First of all, if you point at the code where you're performing the behaviour you expect to happen and walk backwards through it to all the points at which it could be called, you'll see that it's only ever called on start and therefore won't happen at all after the game is up and running. It seems as though this behaviour happens in your "hpBar" function within HealthBar - this is only ever called in Start.
However before we discuss where else to update this value, I've noticed that both HealthBar and Player_UI each own their own HealthSystem instance. When you call new HealthSystem(4)
you are creating a brand new instance of the HealthSystem class, so given that you call it once in HealthBar and again in Player_UI, you'll have 2 separate objects that you use at different points.
So my advice to you is to:
Get rid of the HealthSystem within HealthBar, use only the one within Player_UI. Make that hpBar into "UpdateHpBar" or something and have it as public with an argument for the health value, it can then be updated whenever you change the actual health.
Also, for your own sanity, you might want to remove the Image and Sprite from HealthSystem given they're not the images or sprites you're using for health. If you had plans for them then ignore me! :)
Sorry if I am a bit late, thanks for your suggestion. It works now!
The Image and Sprite from HealthSystem was when I was trying to have the health change from that class. So yeah, they are redundant. Thanks.
Answer by bdubbert · Jan 18, 2021 at 05:49 PM
You need to call HPBar() somewhere, either in update or when your health changes. Right now you only call it in start.
Your answer
Follow this Question
Related Questions
newbie question how would get the percentage of its original starting value over its current value 1 Answer
Calling a variable from another class 2 Answers
Making a hunger script 1 Answer
SendMessage is working but the variable is not being updated on the other end 1 Answer
Semicircular Health/Mana/Progress Bars 0 Answers