Ammo GUI Update in Unity

Sharing is caring:

Use a GUI to Track Ammo Count

In this Unity tutorial we will be using the Coconut Cannon files to add a “Ammo GUI” for our ammo count. We will add some Javascript that will enable us to have a GUI that updates our current ammo count and total ammo available in real time. This is a simple script that checks to see how many coconuts we have to shoot then allows us to reload once we’ve run out of coconuts in our “clip”.

We’ll create our GUIText and align it so that it’s showing up in the lower left corner. You can change the font size and font type to one that you like (I used the “Kaushan Script”) but it is not required.  There’s no need to change the name of the GUI Text as this will all be dynamically changed with out script. Our script will check how much ammo we have, then subtract and add ammo.

Every time we shoot a coconut is deducted from our “coconuts” variable. When we’re out of “coconuts” we’ll reload and deduct “coconuts” from our “totalCoconut” count. Script can be found below along with a link to Google Fonts.


Font File

“Kaushan Script” on Google Fonts.


Javascript Files

var sound : AudioClip; //canon fire soundClip

var reloadSound: AudioClip; //reload soundClip

var coconutSpeed = 100; //force of coconuts being shot

var projectile : Rigidbody; //coconut prefab goes here

var coconuts = 30; //amount of coconuts per "clip"

var totalCoconuts = 60; //total amount of coconuts we have available

var reloadTime = 3; //time to reload

var reloadAmount = 10;

var ammo : GUIText; //GUI text for our coconut/ammo count

function Update () {

Fire ();

Reload ();


function Fire () {

if(coconuts > 0) { //if we have at least 1 coconut then we can fire

if (Input.GetButtonDown("Fire1")) {

var clone : Rigidbody;// Instantiate the projectile at the position and rotation of this transform

clone = Instantiate(projectile, transform.position, transform.rotation);

clone.velocity = transform.TransformDirection (Vector3.forward * coconutSpeed);// Give the cloned object an initial velocity along the current

AudioSource.PlayClipAtPoint(sound, transform.position, 1);                     // object's Z axis

coconuts -=1; //subtracts one coconut per fire sequence




function Reload () {

if(coconuts < 1) {    //if we have less than 1 coconut then we can reload

if(Input.GetKeyDown("r")) {

AudioSource.PlayClipAtPoint(reloadSound, transform.position, 1); //plays reload soundclip

yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding coconuts

coconuts += reloadAmount; //adds 3 coconuts to our "clip"

totalCoconuts -= reloadAmount; //subtracts 3 coconuts from our totalCoconuts amount




function OnGUI () {

ammo.text = "Coconuts: " + coconuts + "/" +  totalCoconuts.ToString();


Jonathan Gonzalez

I love all things in game development and want to contribute by teaching others how to create games. I'm always looking to help others create awesome digital worlds. I've been a hardcore gamer since I was a kid and now it's my turn to create awesome digital experiences.

More Posts - Website

Follow Me:


  1. Hello, I was trying to follow this tutorial but I’m using Unity 5 and when I try to add the updated script for the ammo GUI it gives the error of “UnassignedReferenceException: The variable ammo of Shoot has not been assigned.”. Since I’m using Unity5 is there a different way for me to generate the GUI Text instead since it’s not letting me drag and drop what I have right now.

    • The GUI way of creating text is outdated. There is a new UI system, here is a tutorial I made recently that covers some UI text elements:

      You can still use this script but you’ll need to change a few things. First you’ll need to add the namespace “UnityEngine.UI” at the top. I believe with JS it’s “include UnityEngine.UI” but I don’t use JS anymore so I may be wrong. With C# it’s “using UnityEngine.UI”.

      Second you’ll need to replace the GUI Text variable with “var ammo : Text”. To replace the default text via script you’d then use something like:

      text.ammo = totalCoconuts.ToString();

      Good luck!

  2. help me, my bullets is infinite, 30/-30

    • You need to include a few more lines to check if you’re able to reload based on how much you have available. So you can add in this:
      first you want to check if you have enough ammo to reload:

      first check to see if you have ammo available before reloading:

      if (currentAmmo < magazineSize && totalAmmo > 0 && Input.GetKeyDown (KeyCode.R))

      Next you’ll go through this setup in order to reload ammo based on the reloadAmount and totalCoconut count.

      if (totalCoconuts >= reloadAmount – coconuts)
      totalCoconuts -= reloadAmount – coconuts;
      coconuts = reloadAmount;

      if (totalCoconuts < reloadAmount - coconuts) { coconuts += totalCoconuts; totalCoconuts = 0; }

  3. thanks for the reply but I’m not getting friend, could make it clearer, or give me the script? thank you

  4. thanks for the reply but I’m not getting friend, could make it clearer, or give me the script? thank you

    • Instead of using “if(coconuts < 1)" add in "if(coconuts < 1 && totalCoconuts >= 1)”, that will check if you also have enough coconuts to reload. The rest is stuff you’ll have to experiment with.

      The other lines I provided would be within a reload function.

  5. thank worked 🙂 !!! only you have a problem, if it is pressing R while recharging it recharges several times, help me, thank you.

  6. very good !!! :), But only lack a problem, if I get pressing R during charging, bullets recharge more than it should and even going to negative, only precisso that please, so the script had been perfect, already implemented until the animations in it! ! thank you

  7. Good morning, I would like to know if you have implemetar as to when colliding with a cube, ie ammunition box he increase the total number of bullets , please , this little script and he bumped but does not increase the number of bullets , help me!

    function OnCollissionEnter (collider : Collision) {

    if(collider.gameObject.tag == “bullets”){

    totalBalas += 30;



    • You’re somewhat on the right track but you have a few issues, first you misspelled “Collision” It’s “OnCollisionEnter” and that should’ve popped up as an error in your console window although the console might have thought that was a custom function. The second thing is, you’re looking for something to collide with the object with the tag of “bullets”. So unless your player has a tag of “bullets” nothing will collide with it. Nonetheless I wouldn’t use a collision for this but a trigger. This is what you’re looking for:

      function OnTriggerEnter (col : Collider)
      ammo += 30;


  8. or how do you when colliding with an object I get more bullets ? would like to do this , thank you

  9. this appearing this error, Operator ‘+ ‘ can not be used with the left hand side of type ‘ UnityEngine.GUIText ‘ and the right hand side of type ‘ int’ .

    but after all this operation must go on the script of weapon or in a ammo box, because my goal is when colliding with a box of ammunition he adds somewhat bullet more in total of bullets , thank you for the answer , this already script this more than 200 lines , rsrsrsr .

  10. thank you friend until he finally got what I wanted !!! 🙂

    the script looked like this:

    OnTriggerEnter function ( col : Collider )
    if ( col.CompareTag ( ” Player” ) )

    ArmasScript.totalBullets + = 30 ;

    Destroy ( gameObject ) ;

Speak Your Mind