Created
May 20, 2021 09:28
-
-
Save LiamPerson/debeca5d3c78b51e93b5ede467ad2f10 to your computer and use it in GitHub Desktop.
A very very simple Health controller with constant health drain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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