Objective: Limit the Player’s ability to fire their laser using an ammo system.
To begin we need to create the UI so the player can see how much ammo they have. I will be using the same method in my previous article Here:
In the UI Manager script, we can create a reference to the ammo text we created and assign it in the Unity Inspector:
We can now create a public method that will handle updating the ammo count and change color when the player is out of ammo:
In the Player script, we will need two variables for setting the ammo count and the maximum ammo:
In the Start Method, we can set the max Ammo count to the active ammo count:
Lastly in the Player’s fire laser method, we can decrease the ammo by one when the laser is fired and tell the UI Manager to update the ammo count: