having trouble with ammo reload script. destorys extra ammo

Hello Everyone,
I have a basic script that allows me to reload a gun when it hits 0.

The only problem is that when I reload the gun while it has some rounds left inside, the current amount in the gun gets destroyed and a brand new 6 bullets are put it.
How can I make it that it only reloads up to 6 ammo without destroying the ones already in the gun?

I know there are other things in the script I need to fix as well, but this is the one error I cant seem to figure out. (the rest I’m sure I can figure out)

Any help would be greatly appreciated. Thank you!

here is the script

var bulletType: Rigidbody;
var bulletSpeed = 10;
private var handgunROF = 0.5;

var gunIsHeld : boolean = false;

var playerAmmoCount:ammoCount;
private var handgunFire :boolean = true;

 function Update ()
 function FireHandgun()
 if (!gunIsHeld) 
	if (playerAmmoCount.roundsInRevolver > 0)
	 	if (Input.GetButtonDown("Fire1")&& (handgunFire))
 			clone = Instantiate(bulletType, transform.position, transform.rotation);
 			clone.velocity = transform.TransformDirection(Vector3(0,0, bulletSpeed));
 			Destroy (clone.gameObject, 0.2);
 			playerAmmoCount.roundsInRevolver -=1; //subtracts 1 round from clip
 			yield WaitForSeconds(handgunROF);
 	function ReloadHandgun()
 		if (Input.GetKeyDown("r"))
 				yield WaitForSeconds(playerAmmoCount.reloadtime);
 				playerAmmoCount.roundsInRevolver +=6;
 				if (playerAmmoCount.roundsRevolverAmmo <6)
 					playerAmmoCount.roundsInRevolver = playerAmmoCount.roundsRevolverAmmo;
 					playerAmmoCount.roundsRevolverAmmo -= playerAmmoCount.roundsRevolverAmmo;

 					playerAmmoCount.roundsRevolverAmmo -=6;

I can’t see anything that destroys the bullets that are there, it adds 6 bullets to the the gun when you reload no matter how many bullets you have left in the gun. You need to add in only as many bullets that there is space for. So it is a bad practice to start with adding 6 bullets.

So we need to do a couple of things to get this right:

1: Calculate how many bullets we want to reload based on how many bullets are left in the gun.

2: Clamp that number down to the number of bullets we have left as spare ammo if it is less than the amount we want to reload.

3: Reload the ammo

Bonus 4: Also, no need to run the reload if we have no bullets left or have 6 bullets in the gun.


if(playerAmmoCount.roundsInRevolver < 6 && playerAmmoCount.roundsRevolverAmmo > 0) //Bonus Step 4
    yield WaitForSeconds(playerAmmoCount.reloadtime);

    float reloadAmount = 6 - playerAmmoCount.roundsInRevolver; //Step 1.
    reloadAmount = Mathf.Clamp(1, playerAmmoCount.roundsRevolverAmmo); //Step 2.

    //Step 3.
    playerAmmoCount.roundsRevolverAmmo -= reloadAmount;
    playerAmmoCount.roundsInRevolver += reloadAmount;

Because it is a coroutine, you would probably want to add a bool so you can’t start loading while loading.