Skip to content

Instantly share code, notes, and snippets.

@JCaraballo113
Created February 23, 2019 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JCaraballo113/27ba020094a280cb36e37d740512874c to your computer and use it in GitHub Desktop.
Save JCaraballo113/27ba020094a280cb36e37d740512874c to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthSystem : MonoBehaviour
{
[SerializeField] int healthPoints = 100;
private int maxHealth;
[SerializeField] Image healthBar;
// Start is called before the first frame update
void Start()
{
maxHealth = healthPoints;
}
// Update is called once per frame
void Update()
{
}
public int GetCurrentHealth()
{
return healthPoints;
}
public void Damage(int damage)
{
healthPoints -= damage;
if (healthBar)
{
UpdateHealthBar();
}
if (healthPoints < 0) healthPoints = 0;
}
private void UpdateHealthBar()
{
healthBar.fillAmount = (float)healthPoints / (float)maxHealth;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment