Ammo Count

Marc Frappier
5 min readAug 12, 2023

--

Display The Player’s Ammo Count on Screen

In our game, the Player has unlimited ammo. This means they can shoot as many times as they want for as long as they want. This makes the game less challenging.

In this tutorial, we are going to limit our Player’s ammo to 15 shots and we are going to display how much ammo the Player has on the screen.

We will start with limiting the Player’s ammo to 15 shots.

Since it is in the player script that we manage firing the laser in the FireLaser() method, this is where we will limit the ammo.

First of all, we need to create a variable to hold the ammo the Player currently has. So I will create a variable of type int and name it _ammoCount and set it to 15.

Ammo count variable

Lets scroll down to the FireLaser() method.

Here we have an if else if statement that check to see if the TripleShot is active or not. If it is, we activate the TripleShot so the player shoots three lasers for a specified amount of time. If the TripleShot is not active, the Player can only shoot one laser. We also call upon the sound clip each time we shoot a laser.

FireLaser() method

In order to check if the Player still has ammo, we must check for it in the if statement as well as n the else statement.

For this check to make sense, we need to deduct the lasers fired by the Player from the _ammoCount. So, in the TripleShotActive, we would deduct 3 shots from the total ammo, otherwise we deduct 1.

Checking for ammo

If we save this and play our game, the ammo will effectively drop each time we shoot our lasers until we reach 0 at which point we will not be able to shoot any more lasers.

The problem though, is we have no idea how much ammo we have left. Also, notice how even though we don’t have any ammo, if we press our spacebar, the sound effect still plays as if we had fired a laser.

Lets fix the sound first. This can be done using a simple if statement checking if the _ammoCount is > 0.

Stop playing sound effect if Player runs out of ammo

Now to tackle the issue of a visual aid on our ammo count.

First step is to add a new UI Text object to the Canvas. this is where the Player Score is displayed.

Creating new UI / Text object in Canvas

Select the new Text object and rename it to Ammo_Count. In the Text box, write Ammo: 15. This is just a place holder. Change the font size to 20 and the Color to White.

Configuring the Text object

Next, we need to place the Text under the Score on the Canvas. We do this by selecting the text object Ammo_Count, and with the move tools, align it up under the Score text. You can tweak its exact position with the X and Y positions.

Aligning Ammo text on canvas

Now that it is in place, we need to anchor it to the top-right corner of the canvas so it doesn’t move around when the window is resized.

We do this in the Inspector > Rect Transform

Anchoring text object in canvas

The script that is tied to the Canvas is the UIManager script.

Open up the UIManager script. First thing we need to do is add a variable of type Text.

new ammo variable in UIManager script

Save this and back in Unity, select the Canvas and there should be a new space called Ammo Left in the script. Drag and drop the Text object we created in the Canvas into this slot.

Back in the UIManager script, in void Start() where we set up the _scoreText we can do the same for the ammo count.

Further down in the script, we have a method that is used to update the Player’s score. We can create a similar method to update the ammo count.

This method will be passed an integer with the current ammo count so it can be printed on the screen.

Notice here we are using the method ToString(). This will convert the integer number received into a string so it can be printed to the screen along with the leading string “Ammo: “.

In order for this method to work, we need to call it and pass it the ammo count. This information is contained in the Player script.

Here, we can create a method that will call the UpdatePlayerAmmo method passing it the updated ammo information. I called it UpdateAmmo(int ammo). Notice we added information inside the parenthesis. This is the information that will be sent to UpdatePlayerAmmo().

Since _uiManager contains the Canvas UIManager component in void Start() in order to update the score, we can reuse it here for the ammo count.

Now all that is left to do is to call this local method passing it the current ammo information.

So, we deduct from _ammoCount each time the Player fires their laser, whether it be 1 time or 3 times. Therefore, we can call this new method each time we deduct from _ammoCount.

Calling the UpdateAmmo method

In short, each time a laser is fired, the UpdateAmmo method is called passing it the current number of ammo left. In turn, the UpdateAmmo method calls the UpdatePlayerAmmo method in the UIManager script which will print the current ammo amount to the Canvas screen.

Ammo count down

I hope you enjoyed this tutorial. See you in the next one.

--

--

Marc Frappier

Poultry Scientist by profession, Computer Scientist at heart. After many years in the poultry business, I have decided to give my passion for technology a shot.