Simple limited ammo system
Let’s have a look at implementing a system to limit the amount of ammo a player has as well as a display a warning when the player has run out of ammo:
Set up the Text objects similar to how it is showed in this article.
Open the UIManager script. Add a function to update the UI based on the amount of ammo. Also add a function which will call the coroutine to show the “NO AMMO” warning on the screen:
Open the Player script and add a private int for ammo count:
In the Shoot function subtract 1 from ammo after each shot and call the UpdateAmmoUI function and pass in the amount of ammo. If ammo is less or equal to 0 call the NoAmmo function instead and then return:
Lastly, add the UI objects to the UIManager on the Inspector:
Final result:
Good luck!