Creating Ammo Count Feature in Unity

Objective: set a limited number of laser the player can shoot

To start I declared in the player script an int variable calle “_ammo” to determine the quantity of laser the player can shoot. Then each time the player shoots we decrement the value of the variable by 1 and next I added a conditional to check if the ammo is 0 then the player can not shoot.

The ammo feature is working but the player doesn’t know how many lasers can shoot. So let’s add some UI.

In the top-right part of the screen I created a text element to show the ammo.

Then in the UI Manager script I create a text variable to get the reference to this UI and update the ammo and in the player script we call the function to update the UI each time the player shoots.

Now the player can see the ammo.

To complete this feature I created a game object to play a sound when the player is out of ammo and try to shoot and added a conditional in the shoot function in the player script to play that sound.

And that’s it! The player has a limited number of lasers and the UI and the sound indicates when he is out of ammo.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rusben Guzman

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.