▶ Try this lesson free: https://generalistprogrammer.com/tuto...
Unity health bar and damage system, built from scratch — a Health component, a TakeDamage
method, clamping, events, and a health bar that actually reflects damage. This Unity tutorial
uses correct, current C# (Unity 2022+ / Unity 6) and keeps your health logic completely separate
from your UI, so any enemy, spike, or fall can hurt the player and the bar just updates.
A clean health system is two halves that never touch, and this video builds both:
The Health component — a public int maxHealth set in the inspector, and a private int
currentHealth set to full in Start. Public methods only, so health can never end up in a
broken state.
TakeDamage(int amount) — subtract the damage, clamp with Mathf.Clamp between zero and maxHealth
so health never drops below zero or climbs past the max, then call Die when it hits zero. Plus a
matching Heal method that adds and clamps the same way.
The health bar with fillAmount — set a UI Image to Filled and drive its fillAmount from
current over max. We also show the UI Slider approach with maxValue and value.
The integer-division gotcha — the number one Unity health bar bug. Dividing two ints snaps your
bar to empty at half health. Cast one side to float before you divide and it is fixed.
Decoupling damage sources — raise a UnityEvent from TakeDamage and let the bar listen, so the
Health logic never references the UI. Any damage source calls GetComponent Health then TakeDamage.
Polish — a Die method that disables the object or reloads the scene, and a smooth Mathf.Lerp
drain so the bar glides down after a hit instead of snapping.
Every line of C# is correct and compile-ready, taught through code walkthroughs and diagrams — a
damage-to-bar flow diagram and a float-cast versus integer-division panel. This is the exact Unity
health system real games build on, and it drops straight into a platformer, a shooter, or an RPG.
Full written Unity UI and health bar tutorial with copy-paste code and inspector wiring:
https://generalistprogrammer.com/tuto...
The 30-day plan for beginners — Game Dev Starter Kit ($19):
https://generalistprogrammer.gumroad....
More beginner Unity builds:
https://generalistprogrammer.com/tuto...
— Chapters —
0:00 Every game needs a health bar
0:14 Game Dev Starter Kit ($19)
0:38 What we build (logic vs UI)
1:52 The Health component
3:05 TakeDamage + clamping
4:33 Wiring the bar (fillAmount)
6:00 The integer-division gotcha
7:22 Decoupling damage sources (events)
8:55 Die + polish (smooth lerp)
10:18 Recap
11:04 Watch next + subscribe
— Title variants (A/B testing) —
1. Unity Health Bar & Damage System (UI Toolkit + Slider)
2. Unity Health Bar & Damage System in C# (fillAmount + Events)
3. Unity Health System: TakeDamage, Heal & a Health Bar That Actually Works
#gamedev #unity #unity2d