“Funslingers” Devblog #26 | Creating an Ammo Counter in Unity

Ethan Bristowe
Everdevs Community
Published in
3 min readSep 7, 2021

Hey Devs!

Another feature we can add to make our game little more interesting is giving the player limited ammunition for their revolver. So naturally, we’ll need an ammo counter so the player can keep track of how many shots they have left!

To create this ammo counter, we’ll essentially be combining our shotgun powerup and our money counter! So let’s by start by creating the UI side of this feature.

We can very simply duplicate our Score_text and use it for the Ammo counter, text, I just changed the colors, placed it at the bottom right and changed the text!

But that’s the easy part, next, let’s create a method in our UI Manager that will update our Ammo Counter accurately!

So here, I have reference to the ammo text and we’ll have it update to show the current ammo int each time the method is called. As well as play the same animation as our score text to make the ammo counter feel interactive!

So where do we call this method from, for our Score text, we had the enemies call this method once they were killed by the player. Instead, we’ll have the player call this method directly, since the player script will know how much ammo we have.

To take pointers from our shotgun powerup, the best place to call our Ammo() method will be in our Shoot() method right after the player shoots.

So I made a _pistolAmmo variable which will keep track of how many bullets the player has, will subtract one each time the player shoots, and update our Ammo Counter with that new number. Then, if that variable ever reaches 0, we stop the player from being able to shoot!

And with that, we should have a pretty slick Ammo Counter!

Unfortunately, as of right now, when the player runs out of ammo there is no way to get more. So in the next devblog, we’ll fix that problem!

--

--

Ethan Bristowe
Everdevs Community

Passionate self taught game developer excited to learn and share everything I can about game development!