Sitemap

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!

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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”.

Press enter or click to view image in full size

This is the method that should be called from your “Player” script.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size
Press enter or click to view image in full size

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.

Press enter or click to view image in full size

Now just add this to your “OnTriggerEnter” method…

Press enter or click to view image in full size

And cache the referenced “_uiManager” script.

And boom. You have a collectable ammo refill!

--

--