Forum begins after the advertisement:
[Part 4] Creating a Metroidvania – Health dont update
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 4] Creating a Metroidvania – Health dont update
- This topic has 1 reply, 3 voices, and was last updated 18 hours, 31 minutes ago by
Terence.
-
AuthorPosts
-
October 5, 2025 at 10:02 pm #18887::
my hearts dont update correctly and i dont really know why
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HeartController : MonoBehaviour { PlayerController player; private GameObject[] heartContainers; private Image[] heartFills; public Transform heartsParent; public GameObject heartContainerPrefab; // Start is called before the first frame update void Start() { player = PlayerController.Instance; heartContainers = new GameObject[PlayerController.Instance.maxHealth]; heartFills = new Image[PlayerController.Instance.maxHealth]; PlayerController.Instance.onHealthChangedCallback += UpdateHeartsHud; InstantiateHeartContainers(); UpdateHeartsHud(); } // Update is called once per frame void Update() { } void SetHeartContainers() { for(int i =0; 1 < heartContainers.Length; i++) { if (i < PlayerController.Instance.maxHealth) { heartContainers[i].SetActive(true); } else { heartContainers[i].SetActive(false); } } } void SetFilledHearts() { for (int i = 0; 1 < heartFills.Length; i++) { if (i < PlayerController.Instance.Health) { heartFills[i].fillAmount = 1; } else { heartFills[i].fillAmount = 0; } } } void InstantiateHeartContainers() { for(int i = 0; i < PlayerController.Instance.maxHealth; i++) { GameObject temp = Instantiate(heartContainerPrefab); temp.transform.SetParent(heartsParent, false); heartContainers[i] = temp; heartFills[i] = temp.transform.Find("HeartFill").GetComponent<Image>(); } } void UpdateHeartsHud() { SetHeartContainers(); SetFilledHearts(); } }
View post on imgur.com
October 5, 2025 at 10:04 pm #18888::Hey Levi! It looks like you’re having some trouble with your heart update logic. I see a couple of potential issues in your code that might be causing the hearts not to update correctly. Let’s go through them:
-
Incorrect Loop Condition: In your
SetHeartContainers
andSetFilledHearts
methods, you have the loop condition as1 < heartContainers.Length
and1 < heartFills.Length
. This condition will always evaluate to true, causing an infinite loop. You should change it toi < heartContainers.Length
andi < heartFills.Length
.Here's the corrected code for those methods:
void SetHeartContainers() { for (int i = 0; i < heartContainers.Length; i++) { if (i < PlayerController.Instance.maxHealth) { heartContainers[i].SetActive(true); } else { heartContainers[i].SetActive(false); } } } void SetFilledHearts() { for (int i = 0; i < heartFills.Length; i++) { if (i < PlayerController.Instance.Health) { heartFills[i].fillAmount = 1; } else { heartFills[i].fillAmount = 0; } } }
-
Health Callback: Make sure that the
onHealthChangedCallback
in yourPlayerController
is being triggered correctly whenever the player's health changes. If this callback is not being called, the hearts won't update. -
Heart Fill Logic: If you want to show partially filled hearts (for example, if the health is not a whole number), you might want to adjust the
SetFilledHearts
method to account for that. For example, if you have a half-heart, you could setfillAmount
to0.5
for the appropriate heart. -
Debugging: If the above changes don't resolve the issue, consider adding some debug logs to check the values of
PlayerController.Instance.Health
andPlayerController.Instance.maxHealth
to ensure they are what you expect.
Here's an example of how you might add debug logs:
void SetFilledHearts() { Debug.Log("Current Health: " + PlayerController.Instance.Health); Debug.Log("Max Health: " + PlayerController.Instance.maxHealth); for (int i = 0; i < heartFills.Length; i++) { if (i < PlayerController.Instance.Health) { heartFills[i].fillAmount = 1; } else { heartFills[i].fillAmount = 0; } } }
Try these adjustments and let me know if it helps! If you're still having issues, feel free to share more details about how the health is being modified in your
PlayerController
. Happy coding!October 5, 2025 at 10:23 pm #18891 -
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: