using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class PlayerHealthScript : MonoBehaviour { public float Health; public float MaxHealth = 100; bool Damaged; //HUD GameObject HUD; Text HealthText; Text GameOverText; Image DamageIndicator; void Start () { HUD = GameObject.FindGameObjectWithTag("HUD"); HealthText = HUD.transform.Find("HealthText").GetComponent(); GameOverText = HUD.transform.Find("GameOverText").GetComponent(); DamageIndicator = HUD.transform.Find("DamageIndicatorImage").GetComponent(); Health = 100; HealthText.text = Health + "/100"; } void Update () { //Damage Animation if (Damaged) { DamageIndicator.color = new Color(1, 0, 0, .1f); } else { DamageIndicator.color = Color.Lerp(DamageIndicator.color, Color.clear, 1f * Time.deltaTime); } Damaged = false; } public void TakeDamage(float damage) { Health -= damage; if (Health <= 0) Health = 0; HealthText.text = Health + "/100"; Damaged = true; if (Health <= 0) { Health = 0; gameObject.GetComponent().enabled = false; gameObject.GetComponentInChildren().enabled = false; StartCoroutine(Restart()); } } IEnumerator Restart() { GameOverText.text = "Game Over"; yield return new WaitForSeconds(3f); SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name); } }