Creating Ammo in Unity!

addam davis
5 min readJun 21, 2021

Objective: Create representation for Ammo and implement on the player.

Like everything else in Unity, nothing exists. If we want to implement an ammo system then we need to create a variable to represent ammo, set a limit and an effect that happens if we are out of ammo.

For starters let’s create a variable on the UI so we can see our ammo. Go to our Canvas on the hierarchy, right click and create UI element text. Name it and place it where you want it on the screen.

Once you have your UI element looking as beautiful as you want it to it’s time to start writing code to manipulate it.

open the UI Manager script and create a variable to represent the UI ammo element.

We need a variable for the ammo itself as well.

Inside the start method we are going to set the ammo text with the ammo variable, so it is accurately represented.

Now the UI will show the Ammo: 15 / Current ammo.

We need to set variables for ammo on the player script.

We want to start the game with full ammo so under the start method we want to set the ammo to our max ammo.

Under the update method we already have an if statement for us to know if we can fire or not, we need to add an ammo stipulation to it.

Before
After

Now if our ammo is higher than zero, we will be able to fire, and if we are out of ammo then we can no longer fire. We now need to start subtracting from our ammo whenever we fire.

Under the method you use to shoot your laser, we need to subtract from our ammo whenever we instantiate a laser. I know we’ve talked about realism; however, I currently want to only take one ammo count away if we have the triple shot active.

Now, we need to make a method for the AmmoUpdate that we are calling in the above code.

This is set to accept the int values we used before. If you want to take three ammo count for the triple shot, then you would change the int value to 3 on the is triple shot active true.

This works great; however, we need to update the UI element so the player can see their current ammo.

This method will update the text on the UI with the int values used in the player ammo update, we just need to call this method every time we call the player method.

We now have a working ammo system; however, we only have 15 shots and currently we don’t have any way to get more. Let’s add a reload function. To start we need to create a UI text element for the player to see that they can reload.

You can choose whatever key you wish, I’m currently using the ‘R’ key for something else, so I chose the ‘Q’ key. Set this text to inactive.

On the UI script we need to create a method to activate the reload text.

Now we need to set up sequence that would call on this method. If we go to the player script and in the method, we use to shoot our lasers we need to add an if statement for zero ammo.

I personally use the less than or equal to, incase somehow the ammo goes below zero the game knows that to do with the information, and it doesn’t break the game.

On the update method of the player script, we need to now add the input ‘Q’ key to reload ammo.

I chose to add 15 to ammo instead of adding max ammo because I’m planning on changing the max ammo amount, but I want the reload to give 15. The UI manager amount will need to be updated later, I just wanted to show you the example now.

In the Ammo Update method on the UI script, we need to add the code to reset the reload text to false. This way when the ‘Q’ key reloads it calls the update ammo and resets the reload text to be hidden from the character. This leaves the only time the reload text is viewable is when you need to reload.

This is it, the basics of an ammo system in your game is here. We can go through it later and tighten it up but for now, you have an ammo count, and you have to reload after 15 shots. Don’t be afraid to experiment with your code and I’ll see you in the next tutorial.

--

--