Adding Features: Ammo Count

Austin Hall
5 min readOct 27, 2022

--

Until now, we have had unlimited ammo for our lasers. That is obviously not realistic, so I decided to add a finite amount of ammunition.

Let’s start with the logic and the code, and then we will move into the UI aspects.

To start, we are going to need an ammo count variable. I made this a public static variable so that I could easily access it in the UI Manager script later. I used an integer because I am only working with whole numbers (can’t have half a bullet).

From the picture above, you can see that my count reads 15 on screen.

Since the game doesn’t actually start until the asteroid is destroyed, my on-screen counter was reading zero until after I shot the first shot. The same thing happened when I died and used the in-game function to reload the game. To fix this, I added an awake method to my script and set the count to 15 there.

I placed the awake method above the start method in my script. Not sure if that matters, but logically, it made since to me.

I wanted my initial value to be 15 when the game started, so in the start method I set ammo count to 16. I used 16 so that the shot I fire to start the game does not count against me.

Next, I created a method called AmmoCount().

There are many ways that you can develop this logic. I am going to show you two of them. Both ways are logically identical. The logic states that when the ammo count method is called, subtract one from ammo count and update the display with the new count. Use which ever method makes the most sense to you.

Way #1

In this method I passed in an integer and used the variable name bullets. Inside the method, I set my ammo count variable equal to the value of my ammo count plus my bullets variable.

In my fire laser method, I call the ammo count method and pass a -1(negative one). This subtracted one from the value of my ammo count each time the laser was fired.

Way #2

I created the same method, but I did not use any parameters. Inside the method I used the decrement function to decrease my ammo count each time the method was called.

the UI Manager method call is used to update the display, which we will discuss shortly.

UI Display

Moving over to the UI Manager script, let’s create a TMP_Text variable and call it _ammoText. Make it private and add the Serialize Field attribute.

Below is the End result. There is a lot going on here so lets break it down.

For the ammo I have an Icon(left) I have the text (number) and I have the text background.

First, the Icon.

Right click on the canvas, scroll down to UI and select Image.

name the object Ammo. Under the Image component, there is field called source image. Find whatever sprite you want to use for the icon, import it into unity and drag it into that spot. (The Icon is not necessary, but I used it, so I wanted you to see how I did it)

Next, use the scene view to position your Icon in the UI.

To get the text background we will follow the same process (If you chose not to use an Icon you do not have to create another object for the background).

Right click on the canvas and create the UI image. This time, drag that object onto the Ammo object to make it a child of Ammo.

Just like with the Icon, you can set your image sprite and adjust the location in the UI. Once you have the position set for the background, you can select the parent object and readjust them both simultaneously. This is only possible because the background is a child of the Icon.

For the text, we will create the UI Text object and make it a child of the background.

Right click on the Background object, scroll down to UI and select Text this time.

In the inspector you can adjust the size, font, color and other text settings.

You may have noticed that the Text Input was left blank. That is because we use our script to manipulate the input.

The only thing left to do is assign the text reference on our UI Manager Script.

Select the canvas. On the UI Manager Script component, drag the Ammo Text object (Not the Ammo Parent, or the Background Parent) into the reference block.

The end result is a functioning system that can track your ammo.

--

--

No responses yet