How do I make a gun reload after 30 shots?

Ok, Im currently making a FPS game which involves animations with reloading, firing, and drawing a weapon. I need to make it so that my weapon stops and reloads after shooting 30 bullets. I also want the weapon to stop shooting after all of the clips run out (total of 3). I need help on making this. The script I’m using is below.

var drawAnim : String = "Draw";
var fireLeftAnim : String = "Fire";
var reloadAnim : String = "Reload";
var animationGO : GameObject;


private var drawWeapon : boolean = false;
private var reloading : boolean = false;
 
function Start (){
DrawWeapon();
}
 
function Update (){
 
    if(Input.GetButton ("Fire1") && reloading == false && drawWeapon == false){
        Fire();
        }
       
        if (Input.GetKeyDown ("r") && reloading == false && drawWeapon == false){
    Reloading();
        }
       
        if (Input.GetKeyDown ("1") && reloading == false){
        DrawWeapon();
        } 
    

}
 
function Fire(){
    animationGO.GetComponent.<Animation>().CrossFadeQueued(fireLeftAnim, 0.08, QueueMode.PlayNow);
}
 
function DrawWeapon() {
  if(drawWeapon)
    return;
       
        animationGO.GetComponent.<Animation>().Play(drawAnim);
        drawWeapon = true;
        yield WaitForSeconds(0.6);
        drawWeapon = false;
       
}
 
function Reloading(){
     if(reloading) return;
   
        animationGO.GetComponent.<Animation>().Play(reloadAnim);
        reloading = true;
        yield WaitForSeconds(2.0);
        reloading = false;
}

First, create an int that keeps track of your current ammo count and a bool

var ammoCount : int = 30;      // Use Whatever Value You Like
var emptyMagazine : bool = false;

Then when you call the “Fire” function, decrement the “ammoCount” value by 1

   ammoCount--;

In your Update function or in your fire function, add the lines

if(ammoCount <= 0)
{
    Debug.Log("No ammo left in magazine");
    emptyMagazine = true;
}

if(Input.GetKeyDown(KeyCode.R) && emptyMagazine)
{
    Reload();
}

Also change you Fire calling to

if(Input.GetButton ("Fire1") && !reloading && !drawWeapon && !emptyMagazine)
{
      Fire();
}

// The "!drawWeapon" function does the same as "drawWeapon == false"

Then in your Reload function

ammoCount = 30;    // Or Whatever Value You Selected Up Top
emptyMagazine = false;

This should count the number of bullets you have left in the magazine, which could be useful when making a HUD. Also the gun will not fire if you have no bullets left in the magazine. Hope this helps and good luck with you game!

in your fire method put a counter and check every time whether it reach 30 or not. For Example:

function Fire()
{
           count--;
if(count==0)
{
          //reload your gun(use your own method here)
          count=30;
}
}

ok so we need to modify your code a fair bit here, you dont have anything that controls ammo, so lets add this

int ammo = 30, stock ammo = 90;
// ammo is what will be in your gun and stock ammo will be the stock pile you have in terms of clips etc.

in your fire method

in

 function Fire(){
if(ammo > 0){
     animationGO.GetComponent.<Animation>().CrossFadeQueued(fireLeftAnim, 0.08, QueueMode.PlayNow);

//add
ammo--;  //this will control the ammo in the gun making it reduce everytime you fire
}
else
//reload here if your ammo is not greater than 0
 }

Ok thanks!