Am I missing something when it comes to basic math?

Okay, I have this really small reloading function, that is called when I press ‘R’

int clipAmmo;//Is set to clipSize in Awake ()
int clipSize = 7;
int totalAmmo = 14;

void Reload () {
		if (totalAmmo >= clipSize) {
			clipAmmo = clipSize;
			totalAmmo -= clipSize;
		} else {
			clipAmmo = totalAmmo;
			totalAmmo = 0;
		}//else
	}//Void Reload

Looks good right? Somehow when ever I press ‘R’ to reload, clipAmmo and tottalAmmo sets itself to 0. I have zero clue in what’s going on because this is just simple math. WHAT AM I DOING WRONG?

Any help is appreciated.

The code and the math is fine. So I’m guessing that Reload is getting called repeatedly when you press R, quickly depleting your ammo to 0

Also, this is prettier and easier to read:

 void Reload () 
 {
	// load the clip with whichever is the smallest amount
	clipAmmo = Mathf.Min(clipSize, totalAmmo);
	
	// reduce total ammo by amount filled
	totalAmmo -= clipAmmo;
}

That said, that code doesn’t check if the clip is already full (which is also why you’re depleting ammo on reload). This will fix that:

void Reload () 
{
    // how many bullets can we fill?
	int fill = clipSize - clipAmmo;

	// load the clip with whichever is the smallest amount
	clipAmmo += Mathf.Min(fill , totalAmmo);
	
	// reduce total ammo by amount filled
	totalAmmo -= fill;
}