Space Shooter: Limiting the ammo of the player

Give the player a little challenge!

Timo Schmid
May 8 · 3 min read

What’s a game without a challenge? I can’t name a game here where there was no challenge. Even casual games have challenges. Don’t get me wrong, challenges are something great in games. Let’s add a ammo restriction for the player!

Setting up the Player.cs script

I. Variables
As always, we need variables as basis for the logic. We need one for the maximum allowed ammo and one for displaying the current ammo the player has available.

II. Adapting existing methods
At the very beginning of the game, we should make sure to give the Player the maximum amount of allowed ammo.

Next up, we should make sure the player can only shoot when there is useable ammo left. For this, we simply need to check if the current ammo is greater than zero before we instantiate the laser. If that’s the case, we shoot a laser and substract one from the currently available ammo.

Changes are highlighted in yellow font.

We want to show the current and the maximum ammo as an UI text element. For this to work, we need two new methods, where one will return the currently available amount of ammo and the other to return the maximum allowed ammo.

Setting up the UIManager.cs script

I. Variables
Same procedure here. We need a variable which tells the UIManager what text elements we would like to manipulate.

II. Adapting existing methods
We want to change the text to represent the current available amount of ammo and to state the maximum allowed ammo. Therefore, we need to access the text components of the just created Text variables:

III. Details
Detail work is important! Yes, also in game development. Consider the following things we want to implement:

  • We should assign the text variables inside of the script as well by checking for the GameObject name

If you want to, you can also change the text color. Let’s say we want to change the text color to red when the player has five or less ammo left. For this, we have to access the color property of the ammo “Text” GameObject

End result
Here is the end result with the implemented color changing of the ammo text:

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Timo Schmid

Written by

The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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