Forum begins after the advertisement:


[Part 4] The hearts for my character health aren’t showing

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 4] The hearts for my character health aren’t showing

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #13598
    demilade
    Participant

    Here is my code
    Health controller
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class HealthController : 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()
    {
    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; 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;
    }
    }
    }
    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();
    }
    }
    Relevant Player Controller Code
    [Header(“Health Settings”)]
    public int Health;
    public int MaxHealth;
    public delegate void OnHealthChangedDelegate();
    [HideInInspector] public OnHealthChangedDelegate onHealthChangedCallback;
    [Space(5)]

    #13603
    Terence
    Keymaster

    Do you see any errors on your Console?

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

Go to Login Page →


Advertisement below: