using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class PlayerHealth : MonoBehaviour { public float health; public float maxHealth = 100f; public Image healthBar; private void Awake() { health = maxHealth; } public void TakeDamage(float damage) { health -= damage; healthBar.fillAmount = health / maxHealth; if (health <= 0) { Invoke(nameof(DestroyPlayer), 0f); } } private void DestroyPlayer() { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; SceneManager.LoadScene("Lose"); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter