Minus a var with a var not working?

Hello Unity pros ;)

I have this script:

The only part you kind need to look at is the "Shoot" function, and the other script.

var gunSound : AudioClip;
var rateOfFire = 2.0; 
static var reloadTime = 2.5;
var canReload = true;
static var canMinus = true;
static var haveWeReset = false;
// static var leftoverAmmo = 0;
static var haveWeShot = false;
static var canShoot = true;
static var clipSize = 32;
static var reloading = false;

function Update () 
{

    if(Input.GetButtonDown("Fire1") && canShoot)
    {
    InvokeRepeating("Shoot", .001, rateOfFire);
    }
    if(Input.GetButtonUp("Fire1"))
    {
    CancelInvoke("Shoot");
    }
    if(clipSize == 0)
    {
    CancelInvoke("Shoot");
    }
    if(clipSize < 1 && canReload)
    {
    Reload ();
    }
    if(Input.GetKeyDown("r") && canReload)
    {
    Reload ();
    }
    if(Input.GetButtonDown("Fire2"))
    {
    animation.Play("Ak47 AimDownSight");
    }
    if(Input.GetButtonUp("Fire2"))
    {
    animation.Play("Ak47 notAiming");
    }
}

function Shoot ()
{
clipSize -= 1;
GUIControl.leftoverAmmo += 1; // plus a global var with one.
audio.PlayOneShot(gunSound);
animation.Play("Ak47 recoil");
}

function Reload ()
{
reloading = true;
canShoot = false;
yield WaitForSeconds(reloadTime);
canShoot = true;
reloading = false;
clipSize = 32;
canReload = true;
resetLeftovers (); 
}

function resetLeftovers ()
{
haveWeReset = false;
leftoverAmmo = 0;

}

there may be so part of the script there is not in use, I have not cleaned the script up yet! :P

And then this script:

static var ammoLeft = 96;
static var leftoverAmmo = 0; // += 1 every time the "Shoot" function executes. 

function Update () 
{
guiText.text = "Bullets: " +Gunbehavior.clipSize +"/" +ammoLeft;
if(Gunbehavior.haveWeShot)
    {
    Gunbehavior.clipSize -= 1;
    }
    if(Gunbehavior.reloading)
    {
    guiText.text = "Reloading...";
    ammoLeft -= leftoverAmmo ; // When the gun reloads the ammo Left should be minused leftoverAmmo, but it is not.

    }
}

and again there may be so part of the script there is not in use, sorry ;)

I want the ammoLeft to be minused the leftover Ammo when the gun is reloading, like so:

if(Gunbehavior.reloading) 
{
ammoLeft -=LeftoverAmmo;
}

But the "ammoLeft" variable gets minused many times, so i get something ridiculus like: Bullets: 32/-877;

but it should only be minused ONCE. I am guessing that it minuses the leftoverAmmo every frame the shoot function is execicuted.

But how can i make it so it only gets minused ONCE?

Sorry if this is a long post for a "mabye" simple question, but i cant just get this to work.

Thanks.

Have you not posted all the code or do you never set Gunbehavior.reloading to false?

So it's being reduced by LeftoverAmmo every frame.

You just need to properly set Gunbehavior.reloading to false when you don't want it to be reloading.

In Reload(), you set “reloading” to true, wait for “reloadTime” seconds, and then set “reloading” to false. However, during that whole time, Update() in your other script will still be called every frame, thus subtracting “leftoverAmmo” repeatedly.

Make sure the part where you subtract the ammo gets called exactly once only. The same goes for the “Gunbehavior.clipSize -= 1;” (your scripts doesn’t show us when and where “Gunbehavior.haveWeShot” is set and reset). For example, subtract the ammo in the Reload() function, reset Gunbehavior.haveWeShot immediately after decreasing the clipSize, etc.