How to make an int not pass 0

I am making a zombie shooter and I’m having an issue where when i reload and per say i have a total of 5 bullets left, the clip size is 8 when i reload it will reload 8 bullets setting the total bullets remaining to -3 how can i make my script so it will never pass 0, and if there was say 2-3 bullets left in the stockpile, that when i reload it will only load those 2-3 bullets not a full clip.

Some coding to show where i am at…

        if (reloading)
        {
            canShoot = false;
            reloadTimer -= Time.deltaTime;
            if(reloadTimer <= 0)
            {
                ammoClip -= origClip;
                ammoClip += spareBullets;
                bulletClip = origClip;
                spareBullets = 0;
                reloading = false;
                canShoot = true;
                reloadTimer = startRTimer;
            }
        }

    public void reload()
    {
        if (ammoClip >= 0)
        {
            spareBullets = bulletClip;
            aud.PlayOneShot(reloadSound);
            canShoot = false;
            reloading = true;
            anim.Play("APCReload");
        }
    }

Let me explain… spareBullets is say the clip holds 8 bullets, i shoot 5 and reload, it would have destroyed those 3 unused bullets, but spareBullets takes that number of bullets left in a clip while reloading and adds them to the stockpile…

ammoClip is total bullets.

bulletClip is currently loaded into the gun.

origClip is a memory to how many bullets will fit into a clip.

if (reloading)
{
canShoot = false;
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0)
{
magazineLevel = Mathf.Min(magazineCapacity, remainingBullets);
remainingBullets -= magazineLevel;
reloading = false;
canShoot = true;
reloadTimer = startRTimer;
}
}
public void reload()
{
if (magazineLevel < magazineCapacity)
{
magazineLevel = Mathf.Min(magazineCapacity, remainingBullets);
remainingBullets -= magazineLevel;
aud.PlayOneShot(reloadSound);
canShoot = false;
reloading = true;
anim.Play(“APCReload”);
}
}

Your variable names are very confusing. You should use more descriptive names so it’s more clear what the variable actually stores.

public int clipSize = 10;
public float reloadTime;

public int bullets;
public int clip; 

private float reloadTimer;



void Reload()
{
    if (clip < clipSize && bullets > 0 && !reloading)
    {
        reloading = true;
        reloadTimer = reloadTime;
    }
}

void Update()
{
    if (reloading)
    {
        canShoot = false;
        reloadTimer -= Time.deltaTime;
        if (reloadTimer <= 0)
        {
            int dif = Mathf.Min(bullets, clipSize - clip);
            bullets -= dif;
            clip += dif;
            reloading = false;
            canShoot = true;
        }
    }
}

The important bit is this

int dif = Mathf.Min(bullets, clipSize - clip);

“dif” is the amout of bullets that need / can be added to our current clip. It takes whatever value is smaller, either the remaining bullets or the difference between the “missing” bullets in the clip. So it never adds more than it’s needed to fill the clip and never more than there are remaining bullets left.

So if clip is 3 (so 7 bullets are “missing”) but we only have 5 bullets left in “bullets” we only add those 5 bullets. So bullets becomes 0 and clip becomes “8”.