Modular Health System in Unity

Simon Leen
2 min readOct 16, 2021

Objective: Create a Health System that works for the Player and Enemies.

Since the last post, we created a lovely red enemy cube. Now we want to make a health system that can be used on both the Player and the Enemy. To start create a new script called Health and attach it to both the Player and Enemy. Open the script and create three serialized variables(only two need to be serialized but doing all three make it easier to see it in action in the inspector).

If the health goes below minimum they die and they can have a max health for health pick ups later. Only the min and max need to be set in the inspector as in the Start method we assign the current health to the max.

Finally we need a Damage method that takes in an integer value for how much damage will be taken from the current health. We then reduce the health by that amount and run a conditional if statement to check if the current health has been taken below the minimum health and if so destroy/kill the target(Player or Enemy).

--

--