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

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #18887
    Levi
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    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
    #18888
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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:

    1. Incorrect Loop Condition: In your SetHeartContainers and SetFilledHearts methods, you have the loop condition as 1 < heartContainers.Length and 1 < heartFills.Length. This condition will always evaluate to true, causing an infinite loop. You should change it to i < heartContainers.Length and i < 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;
              }
          }
      }
    2. Health Callback: Make sure that the onHealthChangedCallback in your PlayerController is being triggered correctly whenever the player's health changes. If this callback is not being called, the hearts won't update.

    3. 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 set fillAmount to 0.5 for the appropriate heart.

    4. Debugging: If the above changes don't resolve the issue, consider adding some debug logs to check the values of PlayerController.Instance.Health and PlayerController.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!

    #18891
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Let me know if the AI’s proposed solutions work. If not, I’ll help you out.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: