# 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;
{
ammoClip -= origClip;
ammoClip += spareBullets;
bulletClip = origClip;
spareBullets = 0;
canShoot = true;
}
}

{
if (ammoClip >= 0)
{
spareBullets = bulletClip;
canShoot = false;
}
}
``````

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.

{
canShoot = false;
{
magazineLevel = Mathf.Min(magazineCapacity, remainingBullets);
remainingBullets -= magazineLevel;
canShoot = true;
}
}
{
if (magazineLevel < magazineCapacity)
{
magazineLevel = Mathf.Min(magazineCapacity, remainingBullets);
remainingBullets -= magazineLevel;
canShoot = false;
}
}

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 int bullets;
public int clip;

{
{
}
}

void Update()
{
{
canShoot = false;
{
int dif = Mathf.Min(bullets, clipSize - clip);
bullets -= dif;
clip += dif;
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”.