In our game Erwin’s Timewarp the dog Erwin is very hungry. His hunger increases and this is visualized by the healthbar – so our bar is a kind of hunger bar that is increasing each second.

In this video you can see the healthbar in action:

The bar is made with Unity 4.6. UI system. The outer control is a UI.RectTransform, the bar is a UI.Slider:

Healthbar UI Unity 4.6

Healthbar UI Unity 4.6

Unity UI Slider component

Unity UI Slider component

The size of the slider can be set by the value property.

To change the healthbar during the game I created an empty gameobject called GameManager. I attached a script to it in which I change the slider every second and also display the current hunger status as Text (inner control UI.Text):

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;

public class GameManager : MonoBehaviour
{
    private static GameManager _instance;

    public static GameManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = GameObject.FindObjectOfType<GameManager>();
                DontDestroyOnLoad(_instance.gameObject);
            }
            return _instance;
        }

    }

    void Awake()
    {
        if (_instance == null)
        {
            _instance = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            if (this != _instance)
                Destroy(this.gameObject);
        }

        if (HealthPanel != null)
        {
            _healthSlider = HealthPanel.Find("HealthSlider").GetComponent<Slider>();

            _healthText = HealthPanel.Find("HealthText").GetComponent<Text>();

            _maxHunger = _healthSlider.maxValue;
        }

    }

    public void Start()
    {
        _hungerSeconds = 0;

        // Invoke the CountSeconds every second to count the passed seconds
        InvokeRepeating("CountSeconds", 1.0f, 1.0f);
    }

    public void Update()
    {
        if (_healthSlider != null && _healthText != null)
        {
            _healthSlider.value = (float)_hungerSeconds;

            _healthText.text = String.Format("Hunger: {0} | {1}", _hungerSeconds, _maxHunger);  

            // TODO: If _hungerSeconds >= maxHunger --> Death ...
        }
    }

    void CountSeconds()
    {
        _hungerSeconds++;
    }

    // Slider of the health panel
    private UnityEngine.UI.Slider _healthSlider;

    // Text to display the health / hunger
    private UnityEngine.UI.Text _healthText;

    // Seconds passed...
    private float _hungerSeconds = 0.0f;

    // ... til the hunger reaches maximum -> death
    private float _maxHunger = 0.0f;
    
    // Health / Hunger panel
    public RectTransform HealthPanel = null;

}

It is important to add the HealthPanel RectTransform to the GameManager to get access to the slider and text components:

GameManager singleton Unity 3D

GameManager singleton Unity 3D

In the next tutorial I will show you how to play a death animation when the hunger excceeds the maximum. Please join our channel to see the progress of the Erwin’s Timewarp game and to be informed about new tutorials.

(Visited 64 times, 1 visits today)