In any game with combat, characters need to take damage and eventually die. A health system tracks this. It determines how many hits a player or enemy can survive before being defeated.
Basic Health Script
This script handles taking damage, healing, and death.
using UnityEngine;
public class Health : MonoBehaviour
{
public int maxHealth = 50;
private int currentHealth;
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int amount)
{
currentHealth -= amount;
if (currentHealth <= 0)
{
Destroy(gameObject);
}
}
public void Heal(int amount)
{
currentHealth += amount;
maxHealth = currentHealth;
}
}
Output:

- TakeDamage() reduces health and destroys object when zero. Heal() restores health.
- Mathf.Clamp() keeps the value between 0 and maxHealth (so health never goes above max or below 0).
Connect Shooting to Health
Once you have a health script, connect it to your shooting system.
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit, range))
{
Health health = hit.transform.GetComponent<Health>();
if (health != null)
{
health.TakeDamage(damage);
}
}
}
Get the Health component from the hit object and call TakeDamage() with your weapon's damage value.
Simple Health Bar
A health bar gives players visual feedback of their remaining health.
public class HealthBar : MonoBehaviour
{
public Health health;
public Slider slider;
void Start()
{
slider.maxValue = health.maxHealth;
slider.value = health.currentHealth;
}
void Update()
{
slider.value = health.currentHealth;
}
}
The slider's max value equals maxHealth. Every frame, the slider updates to match current health.
Healing Pickup
Health pickups allow players to restore health during gameplay.
using UnityEngine;
public class HealthPickup : MonoBehaviour
{
public int healAmount = 20;
public Health health;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
if (health != null)
{
Debug.Log($"Health Before Heal: {health.maxHealth}");
health.Heal(healAmount);
Debug.Log($"Health After Heal: {health.maxHealth}");
Destroy(gameObject);
}
else
{
Debug.Log("Health script NOT found");
}
}
}
}
Output:
When the player touches the pickup, heal them and destroy the pickup object.