Skip to content

Instantly share code, notes, and snippets.

@LiamPerson
Created May 20, 2021 09:28
Show Gist options
  • Save LiamPerson/debeca5d3c78b51e93b5ede467ad2f10 to your computer and use it in GitHub Desktop.
Save LiamPerson/debeca5d3c78b51e93b5ede467ad2f10 to your computer and use it in GitHub Desktop.
A very very simple Health controller with constant health drain
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // Import the UI module.
public class HealthController : MonoBehaviour
{
public Slider HealthBar;
public Text HealthBarTextValue;
/// ### PROPERTY ###
private float _health = 100; // The real value
public float Health // The accessible value
{
get => _health; // When we get the health, give them the real value.
set
{
// Update the real value.
_health = value;
// Update the GUI / HUD.
HealthBar.value = _health;
HealthBarTextValue.text = ((int) _health).ToString();
}
}
void Update()
{
Health -= Time.deltaTime * 10; // Drain our health at 10 point per second.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment