Stop checking for key input

Hello all. In the game I’m helping develop, we are trying to make an ammo system, but there’s one problem. I have no idea how I would start and stop checking for input when a certain condition is met. Say I wanted to stop checking for input because they ran out of clips of ammo or because they need to reload. I was thinking that I could make a boolean (In the code you’ll see that I did, it’s called “CanShoot”) and then say that boolean is false but I don’t know how to connect the dots. Thank you for your help.


if ((Input.GetButton("Fire1") && (M4A1.activeSelf || M9.activeSelf) && Time.time > nextFire)) //Allowing the user to hold down or single click the mouse button.
                {
                    if (M4A1.activeSelf || M9.activeSelf && Input.GetButton("Fire1"))
                    {
                        if (M4A1.activeSelf)
                        {
                            M4A1_Ammo.gameObject.SetActive(true);
                            M9_Ammo.gameObject.SetActive(false);
                            CurrentM4A1Ammo -= 1;
                            Vector3 M4A1BulletPosition = new Vector3(-0.232f, 0.3f, -0.067f);
                            Instantiate(M4A1Bullet, BulletPosition, Quaternion.identity);
                            FinalM4A1Ammo = CurrentM4A1Ammo;
                            M4A1_Ammo.value = CurrentM4A1Ammo;
                            print("M4A1 ammo is " + CurrentM4A1Ammo);
                            if (CurrentM4A1Ammo == 0)
                            {
                                CanShoot = false;
                                M4A1Clips -= 1;
                                print("M4A1 clips: " + M4A1Clips);
                                M4A1_Ammo.value = CurrentM4A1Ammo;
                                if ((Input.GetButton("Fire1") && (M4A1.activeSelf || M9.activeSelf) && Time.time > nextFire))
                                {
                                    if (M9.activeSelf)
                                    {
                                        M9_Ammo.gameObject.SetActive(true);
                                        M9_Ammo.gameObject.SetActive(false);
                                        CurrentM9Ammo -= 1;
                                        // Instantiate(M4A1Bullet, BulletPosition, Quaternion.identity);
                                        FinalM4A1Ammo = CurrentM4A1Ammo;
                                        M4A1_Ammo.value = CurrentM4A1Ammo;
                                        print("M9 ammo is " + CurrentM4A1Ammo);
                                    }
                                }
                                if (M4A1Clips == 0)
                                {
                                    // Disallow reload here.
                                    // Disallow Fire1 here.
                                }
                            }
                            if (CanShoot == false)
                            {
                                // This is where you would disable checkforinput.
                            }
                        }
                        if (M9.activeSelf)
                        {
                            M9_Ammo.gameObject.SetActive(true);
                            M4A1_Ammo.gameObject.SetActive(false);
                            CurrentM9Ammo -= 1;
                            Vector3 M9BulletPosition = new Vector3(-0.232f, 0.3f, -0.067f);
                            // Instantiate(M4A1Bullet, BulletPosition, Quaternion.identity);
                            FinalM9Ammo = CurrentM9Ammo;
                            M9_Ammo.value = CurrentM9Ammo;
                            print("M9 ammo is " + CurrentM9Ammo);
                            if (CurrentM9Ammo == 0)
                            {
                                M9Clips -= 1;
                                print("M9 clips: " + M9Clips);
                                M9_Ammo.value = CurrentM9Ammo;
                                if ((Input.GetButton("Fire1") && (M4A1.activeSelf || M9.activeSelf) && Time.time > nextFire))
                                {
                                    if (M9.activeSelf)
                                    {
                                        M9_Ammo.gameObject.SetActive(true);
                                        M4A1_Ammo.gameObject.SetActive(false);
                                        CurrentM9Ammo -= 1;
                                        // Instantiate(M4A1Bullet, BulletPosition, Quaternion.identity);
                                        FinalM9Ammo = CurrentM9Ammo;
                                        M9_Ammo.value = CurrentM9Ammo;
                                        print("M9 ammo is " + CurrentM9Ammo);
                                    }
                                }
                                    if (M9Clips == 0)
                                {
                                    // Disallow reload here.
                                    // Disable fire1 here.
                                }
                            }
                        }

It should be as simple as making a bool and encapsulating your input check inside of an if statement like so.

public bool canFire;

void Update()
{
   if (canFire)
   {
      if ((Input.GetButton("Fire1") && (M4A1.activeSelf || M9.activeSelf) && Time.time > nextFire))
      {
         //Your firing method.
         if (ammo <= 0)
         {
            canFire = false;
         }
      }
   }
}

You can try putting the entire code into a if statement(May not be the best solution but something like so). So basically the key input will be only checked when certain conditions are met.

For example:

private bool Canshoot;
private int curBullet = 10;

void Update()
{
    if (CanShoot)
    {
        if (Input.GetButtonDown("Fire1"))
        {
            curBullet -= 1;
        }
    }
    else
    {
        return;
    }
    if (curBullet == 0)
    {
        CanShoot = false;
    }
    else
    {
        CanShoot = true;
    }
}

I don’t know why you are trying to do it so complicated but Ok…
A tip I can give you that please don’t use too many “IF” statements. it will make your code dirty…