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.

And boom. You have a collectable ammo refill!

--

--

--

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What is DevSecOps?

Application of Automata theory in Compiler Design

Programming History: The Influence of Algol on Modern Programming Languages (Part 2)

Designing Accessible Builder Apps

A decorative header image that is a geometric pattern in shades of blue.

A Deep Dive into Airbnb’s Server-Driven UI System

Deploying Flask Application on VPS Linux Server using Nginx

Building the Nativase Web App — Motivation & Methodology

Serverless Puppeteer on AWS

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
Adam Reed

Adam Reed

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

More from Medium

Making nice cameras in Unity with the Cinemachine plugin

Variables! — The building blocks of programming

Journal 142 — Unity Project Development, 2.5D Shooter

Zoo Cards as Scriptable Objects