Steps
1. Created a UI Canvas, text, and buttons.
2. Position the elements on the canvas.
3. Instantiate the variables of addHealth, removeHealth, healthDisplay, and health initialized at 5.
4. Start function adds listener for both removeButton and addButton. New functions for both as well.
5. On each click, health is either decremented or incremented based on which button was clicked. Only changed if the health value is still within range. 
6. After changing the health variable, healthDisplay text is updated to represent the change. 
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class Logic : MonoBehaviour
{
    public Button removeButton;
    public Button addButton;
    public TMP_Text healthDisplay;
    public int health = 5;
    // Start is called before the first frame update
    void Start()
    {
        healthDisplay.text = health.ToString();
        removeButton.onClick.AddListener(removeHealth);
        addButton.onClick.AddListener(addHealth);
    }

    void addHealth()
    {
        if (health < 10)
        {
            health++;
        }
        healthDisplay.text = health.ToString();
    }
    void removeHealth()
    {
        if (health > 0)
        {
            health--;
        }
        healthDisplay.text = health.ToString();
    }
}
Back to Top