Creating An Ammo Refill Object In Your Game
This is part 2 of my last article on “Creating Limited Ammo In Your Game”.
Now that you have an ammo system that allows you to limit the number of shots that the player can fire you will most likely want some way to refill that ammo count when triggered.
So let’s make a game object that refills your ammo!
You will need to create a new game object and add these components to it.
You can use 3D objects instead of 2D based on what your game requires. But doing so will use 3D components as well so make sure to take that into account.
The script at the bottom “Ammo Refill” is a new script for this object.
This is the code you will want to place inside of your “Ammo Refill” script.
This script has a reference to the “Player” script as well and calls a method which for this example is named “RefillAmmo”.
This is the method that should be called from your “Player” script.
If you wanted to add a visual UI text object to display your current ammo count as I did in the GIF above…
Then create a new “UI>Text” object in your hierarchy. If you don’t already have a canvas then one will be created. Make sure that this object is a child of the canvas that’s active in your scene.
Next, you will need to create a new script and add it to the above-mentioned canvas.
This script will act as a sort of manager for your UI.
Now just add this to your “OnTriggerEnter” method…
And cache the referenced “_uiManager” script.