Galaxy Shooter: Bullet counter

Esteban Ibarra
3 min readApr 27, 2021

Challenge 2 of the core programming challenge is “Visualize on screen the ammo count of the player in the form of current/max”. I took this as instead of a health bar, we’ll be showing the amount of ‘bullets’ or lasers in our case and have them reduce in number visually as you shoot them.

As before, I kept things simple and created a new image on the canvas and placed the laser sprite into it, resized to a small bit, and prefabbed it as “AmmoCount_N_” in case I wanted to instantiate bullets as N_00, N_01, etc.

Once the image is prefabbed, we can delete it off the canvas. Also it’s always a good idea to 0 out the x and y coordinates.

Now we’re ready to redo the UpdateAmmoCount in the UIManager!

Since we’re redoing the healthbar, we can just delete or comment out the older code. I commented it out for sentimental reasons. :)

Next I created a routine to initialize the bullets. Since we don’t know if the maximum bullet count will change in the future, I wanted it to be dynamic so I created a for loop that checked up to a variable called MaxAmmo.

inside the for loop, there’s an array of canvas Images called ammoBits that first gets instantiated. Then we set the parent of that image to an empty inside of the canvas that’s located right under the bullet text that’s attached to _AmmoHolder. Since now we only have to worry about the placement in the holders local axis, we can set the first position at 0, and every subsequent bullet 4 units next to one another. After they’re all placed in the UI, we turn them off. We’ll turn them back on in just a moment:

This is where all the action is! We first initialize the ammo count at the Start() but then every time the player fires.

We first grab the ammo count from the player, and then we update the text UI to show the number of bullets left, then we go through 2 for-loops to turn on the appropriate number of bullets; The first for loop cycles through the entire bullet collection through _maxAmmo. This assures us a true clean slate for the next for loop where we’ll go through another for loop that cycles to the current _ammoCount and turns on each ammoBit in the array until we reach the current ammo Count.

Looks good! Tomorrow we’ll make a simple wave spawner.

--

--