Home and Learn: Games Programming Course

Enemy and Player Health Scripts

In the previous lesson, we did some raycasting so that we could fire on the enemy soldier. In this lesson, we'll write a script to set a value for the enemy. Then, whenever the player shoots the enemy, we can deduct points from the health value. When the enemy's score reaches zero, we can play the soldier's dying animation. We'll also write a similar script for the player's health. Let's get started.


Enemy Health

In the Project area at the bottom of Unity, click on your Scripts folder. Right-click inside the Scripts folder. From the menu that appears, select Create > C# Script. When the script is created, type a new name for it. Call it EnemyHealth. Then double click the script to open it up in your coding editor.

You can delete the Update method (and any comments) and the Start method because we don't need them, at the moment. Now add these two variables inside the curly brackets of the class:

public int currentHealth = 20;
public static bool isEnemyDead = false;

The first one is an integer called currentHealth. We'll use this to keep track of the enemy's health. We've made it public because this script can be attached to any other enemy we add. The new enemies can then have their own values set. For example, you might want to set a higher value for tougher enemies. This higher value will then override the default value of 20 that we've set.

The second variable is a Boolean called isEnemyDead. It's set to false. We'll set it to true when the enemy bites the dust. But notice that it's public and static. That's because we'll need to access it soon from another script. (However, public static variables can be hard to keep track of and cause bugs in your code, so we'll change this in a later tutorial. It's OK for now, though.)

Now add this method to your code, just below the variables:

public void TakeDamage(int damage)

This is a method that we've called TakeDamage. We want to pass in an int value. The variable name we've come up with for this is damage. The method is void because we don't need to return a value. Notice that the method is public. That means it will be able to be seen from outside of this class, which is what we want as we're going to be calling it when the player shoots the enemy.

As the code for your TakeDamage method, add this between the curly brackets, then we'll run through it:

currentHealth = currentHealth - damage;

if (currentHealth <= 0 && isEnemyDead == false)

Debug.Log("DEAD: " + currentHealth);
isEnemyDead = true;


The if statement checks two things: if the value inside of the currentHealth variable is less than or equal to zero, and if the Boolean variable isEnemyDead is false. If both of those things are true then the code for the if statement gets executed. The first line is just a debug line, so you can see the value of currentHealth. The next line is this:


We're going to attach this script to the Soldier. This will then be the gameObject. We then get the Animator component attached to the Soldier. This, remember, holds all our animations. We can then Play the animation called Dying. (The name of the animation you want to play needs to go between two double quotes.)

The final line sets isEnemyDead to true. We need to do this so the enemy won't fire after he's dead.

But the code in your editor should look like this:

Unity C# code to reduce an enemy's health

Now go back to your GunFire script. Inside of your GunshotHitCheck method, add this line just below the Debug line for "Hit: " + damage: (The new line is in bold.)


Your GunshotHitCheck method should look like this:

Unity C# t0 call a method in another script

The hit object (which would be the enemy) has a collider object. The collider allows you to get a component. In between the left and right pointy brackets of GetComponent, you can type the name of a script. In this case, we're typing the name of that EnemyHealth script we've just created. Type a pair of round brackets then a dot and you can select any public method or variable that's in the EnemyHealth script. We want to call that TakeDamage method. We hand it the value of damage, which we set up at the top of the GunFire script and is set to a value of 5.

Save your code and go back to Unity. The Enemy Health script is not attached to anything yet. To attach it to the Soldier, you can either drag and drop the script onto Soldier in the Hierarchy or drag and drop the script onto the Inspector with the Soldier selected (drag and drop above the Add Component button.) The image below shows both:

Dragging a health script onto a game object

Notice the Current Health value in the Inspector on the right, in the image above. It has a default value of 20, which is the value we added when setting up the variable. You can change that to any positive number you like.

Now play your game. Shoot the Soldier a few times. You should see the animation play when you've got him. The short video below shows that, and also highlights a problem we need to solve.


The problem is that even though the soldier is dead, he's still trying to follow us!

To fix the issue, we can use the isEnemyDead Boolean in another script, which is why we set it up as public and static (we'll be changing this later). So, go back to your EnemyLookAt script. Change it from this:

void Update()



To this:

void Update()

if (EnemyHealth.isEnemyDead == false)




We've wrapped the single line of code we had in an if statement:

if (EnemyHealth.isEnemyDead == false)

To reference another script, you can use the name of the script. Previously, in GunFire, we used GetComponent<EnemyHealth>(). That's because we didn't have a simple static variable to access but a public method. This time, we just need to access the isEnemyDead variable from EnemyHealth. We only want the enemy to look at the player if isEnemyDead is false.

Save the script and go back to Unity. Play your game and you should find that the dead soldier doesn't try to follow us, as shown in the short video below:


OK, now that we can kill the enemy, let's write code so that the enemy can kill us.


Player Health

We want the player to take damage every time it's hit. So the code is pretty much the same as when the enemy gets hit. In your Scripts folder, then, right click and select Create > New C# Script from the menu. Call your new script PlayerHealth. Double-click your new PlayerHealth script to open it up in your coding editor. Now set up two variables at the top of your code, inside the curly brackets of the class. These two:

public int maxHealth = 100;
public int playerHealth;

The first variable is called maxHealth. We've set it to a value of 100. But you can have anything you like here. The variable type is int. The int type stores whole numbers. It's a public variable, which means you can change it in the Inspector in Unity, if you want.

The second variable is also an int. It's called playerHealth. We'll deduct from this every time the player is hit. (The variable's only public so we can see what's happening in Unity. Later, we'll display the player's current health on screen.)

In your Start method, add this line:

playerHealth = maxHealth;

When the game starts, we just set the playerHealth to be the same value as the maxHealth.

Now we'll add a method that the enemy can call, if it hits the player. Add this method to your code, just below the Start method: (You can delete the Update method.)

public void PlayerTakeDamage(int damage)

playerHealth = playerHealth - damage;


The method is called PlayerTakeDamage. In between the round brackets of the method, we're passing over an int value that we've called damage. We did exactly the same for the enemy.

As the code for the method, we're deducting the value of damage from the player's current health. Your code should look like this:

Unity C# code to reduce a player's health

Save your code and go back to Unity. Now drag and drop your PlayerHealth script onto the First person controller item in the Hierarchy:

Dragging a player health script onto a game object

With the First person controller selected, have a look at the Inspector. You should see the two public variables we added, Max Health and Player Health:

The player health script in the Inspector

Also, notice that the Tag at the top says Player. Click the dropdown for Tag and set it to Player, if you haven't already. (Don't worry if your Player Health textbox says 0. It will read 100 when your game loads.)

OK, now we can write the code that makes the Soldier fire at the Player. The Player will take damage and we'll see the PlayerHealth variable in the Inspector above decrease every time the Player is shot. We'll do that in the next lesson below.

<--Back to the Unity 3D Course Contents Page

Email us: enquiry at homeandlearn.co.uk