Modular Health System in Unity!

Objective: Create a health system that can be used for everyone!

Today we are working on a Universal Health System. This is a simple and quick system to add to your game to give health and handle damage for all the characters in your game.

We begin by creating a “Health” script. This is a modular script, so it can be attached to player, enemies, and objects.

We need 3 int variables. One for max health value, minimum health value, and current health value.

In the void start set the current health to the max health value.

Now we can create a damage method. The damage method should have an int parameter for the damage amount.

When this method is called the current health should be subtracted by the damage amount. Also, it should check if the current health is lower than the minimum health value, and if it is, destroy the object.

When the player shoots it needs to check for a Health component.

If the health variable is not null, meaning the object has a health component, then it needs to deal damage.

We serialized the variables so we can assign them in the inspector.

Now you can shoot anything with a health component, and it will take damage until its health is below its minimum value then it will be destroyed.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store