Run out of ammo

How can i make if i have in magazine 30 ammos and in storage for example 100.
So if i shoot one ammo so its 29/100 and when i press R to reload and set 30/99
Here’s my gun script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour
{
    public float damage = 10f;
    public float range = 100f;
    public float fireRate = 15f;
    public float impactForce = 30f;
    public Camera fpscam;
    public ParticleSystem muzzleFlash;
    public GameObject impacteffect;

    private float nextTimetoFire = 0f;
    private bool isReloading = false;

    //AMMO
    public int MaxAmmo = 10;
    public int currAmmo;
    public float reloadTime = 1f;

    void Start()
    {
        currAmmo = MaxAmmo;
    }

    // Update is called once per frame
    void Update()
    {
        if(isReloading)
            return;

        if(currAmmo <= 0)
        {
            StartCoroutine(Reload());
            return;
        }

        if(Input.GetButton("Fire1") && Time.time >= nextTimetoFire)       
        {
            nextTimetoFire = Time.time + 1f/ fireRate;
            Shoot();
        }
    }

    void Shoot()
    {
        muzzleFlash.Play();

        currAmmo--;
        RaycastHit hit;
        if (Physics.Raycast(fpscam.transform.position, fpscam.transform.forward, out hit, range))
        {
            if(hit.transform.tag == "Enemy")
            {
                CharacterStats enemyStats = hit.transform.GetComponent<CharacterStats> ();
                if(enemyStats == null)
                {
                    Debug.Log("You didn't hit an enemy");
                } else
                {
                    enemyStats.TakeDamage(damage);
                }
                if(hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(-hit.normal * impactForce);
                }

                GameObject impactGo = Instantiate(impacteffect, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(impactGo, 2f);
            }
        }
    }
    IEnumerator Reload()
    {
        isReloading = true;

        Debug.Log("Reloading...");

        yield return new WaitForSeconds(reloadTime);

        currAmmo = MaxAmmo;
        isReloading = false;
    }
}

Also here’s my discord so if you need any more information and i,m not answering here:
Perodix__™#0001

Create a variable to track your ammo storage amount:

int ammoStore = 100;

When your Reload coroutine executes, subtract currAmmo from MaxAmmo to get the value of how much ammo was used, then add that value to currAmmo and subtract that value from ammoStore.
You’ll also want to cap the used-ammo variable so that it cannot be greater than the current value of ammoStore, otherwise currAmmo would be set to a greater value and ammoStore would be set to a negative value.

int ammoUsed = MaxAmmo - currAmmo;

if(ammoUsed > ammoStore)
{
  ammoUsed = ammoStore;
}

currAmmo += ammoUsed;
ammoStore -= ammoUsed;

Why not? You already created the forum account; use the forum.
You can’t expect others to chase you around other communication platforms.

2 Likes

Thanks i,m gonna try that and let you know if there’s error?
Are you have discord to me add you ?

Is work ty