In today’s article, we’re going to add a new ammo pickup item that will replenish the ammo of the player. In order to have a need for this pickup, we need to take ammo away from the player when they shoot. We will set the player with an amount of ammo they can shoot before running out, and display that on the screen as well. Let’s get right to it.

First we need to create our HUD item and display the ammo count on the screen. To do this we will duplicate our ‘Score_Text’ game object in the ‘Canvas’ parent. We will rename it ‘Ammo_Count_Text’ and position it wherever we want on the screen. I placed mine in the bottom right corner for now and anchored it in that area as well.

Now we will go straight to scripting the behaviors we’re looking for. We will start in the ‘UIManager’ script and create a new serialized private Text called ‘_ammoText’.

We will now create a new public function in this script called ‘UpdateAmmoCount’. We need to set a couple of parameters. Both will be integers, one will be named ‘ammoCount’ and the other ‘maximumAmmo’. Next, we will set the text component of our ‘_ammoText’ to display whatever we’re wanting it to say. I’m going with Ammo: and will append in the ammoCount variable then append the maximumAmmo variable.

Next, we write an if statement saying if ‘ammoCount’ is equal to 0, then we will change the text color to red. Else, the text color will be white. Now go back to Unity and drag the ‘Ammo_Count_Text’ object to the field we created for this script on the ‘Canvas’ game object.

We will now head to the ‘Player’ script and create two private integers. The first will be named ‘_ammoCount’ that will be set to 15 and also serialized. The second, named ‘_maximumAmmo’. We will also create a private bool named ‘_canPlayerShoot’ and set it to true.

In ‘Start’ we will set ‘_maximumAmmo’ equal to ‘_ammoCount’.

In the ‘ShootLaser’ function we will add everything we currently have inside an if statement. It will read, if ‘_ammoCount’ is greater than 0, then do all the things we were doing before, but we will set ‘_canPlayerShoot equal to true at the top of this if statement to ensure it will work properly.

At the end of what we had before, we will add a line to subtract 1 from ‘_ammoCount’ when the input is pressed. We will also run the ‘UpdateAmmoCount’ function from our ‘UIManager’ script here. We will pass in the new variables we created today. After all that is called, then we end the if statement but continue the logic with an else. And we just set ‘_canPlayerShoot’ to false.

If you play now, you will see that you have a HUD display and once you reach 0, the text will turn red and you won’t be able to shoot any longer.

Now, we will add the pickup. It’s the exact same thing we have been doing with the shields, health, and triple shot, so I will refrain from going into that detail again and go straight to the scripting.

In the ‘Player’ script we will create another new public function called ‘AddAmmo’. In this function, we will set ‘_ammoCount’ equal to 15. We will then update the ammo count in the UI like we just did.

In our ‘Powerup’ script, we will add another case and call the ‘AddAmmo’ function we just created.

And finally, in the ‘SpawnManager’ script increase the number in the random range of the Instantiate by 1. Mine is now at 5.

Make sure you add this new pickup to the ‘Spawn_Manager’ game object, then test it out. When you collect the ammo pickup, you should see your ammo count raise back to 15 and continue to be able to shoot.