How to disable a function?

I have my script for a grenade, but I want it to disable if i’m out of grenade. I have most of it figured out, just can’t figure out how to disable my function. Here is my grenade throwing script:

public float throwForce = 40f;
public GameObject grenadePrefab;

public int maxAmmo = 2;
private int currentAmmo;

void Start()
{
    currentAmmo = maxAmmo;
}

// Update is called once per frame
void Update ()
{
    if (currentAmmo > 0)
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            ThrowGrenade();
            currentAmmo--;
        }
    }

    if(currentAmmo <= 0)
    {
       
    }
    

}

void ThrowGrenade()
{
   GameObject grenade = Instantiate(grenadePrefab, transform.position, transform.rotation);
    Rigidbody rb = grenade.GetComponent<Rigidbody>();
    rb.AddForce(transform.forward * throwForce, ForceMode.VelocityChange);
}

Add here is my actual grenade script:

public float delay = 3f;
public float radius = 5f;
public float force = 200f;



public GameObject explosionEffect;

float countdown;
bool hasExploded = false;
   	
void Start ()
{
    countdown = delay;
}


void Update ()
{
    countdown -= Time.deltaTime;
    if(countdown <= 0f && !hasExploded)
    {
        Explode();
        hasExploded = true;
    }
}

void Explode()
{
    GameObject explosion = Instantiate(explosionEffect, transform.position, transform.rotation);

    Collider[] colliders = Physics.OverlapSphere(transform.position, radius);

    foreach (Collider nearbyObjects in colliders)
    {
        Rigidbody rb = nearbyObjects.GetComponent<Rigidbody>();
       if(rb != null)
        {
            rb.AddExplosionForce(force, transform.position, radius);
        }
    }
    

    Destroy(this.gameObject);
    Destroy(explosion, 2f);
}

Even if you could do that it wouldn’t make sense.

The way it’s written is fine. Should work as is and only throw if there’s more than 0 grenades.

Just curious why you even want to “disable” it . You can’t but why would you want to?

The line:

 if (currentAmmo > 0)

Should already be preventing you from trowing grenades when you are out of ammo. There is no further steps you need to take to disable your code. I feel we are missing some detail. I assume the first script is on your player and the second one on the grenade, what happens exactly when you run the code? (And the others are right you don’t “disable” a method, but you can disable a script component or the gameobject it’s running on)

Try to add another preventing method:

 void ThrowGrenade()
 {
if(currentAmmo >0){
    GameObject grenade = Instantiate(grenadePrefab, transform.position, transform.rotation);
     Rigidbody rb = grenade.GetComponent<Rigidbody>();
     rb.AddForce(transform.forward * throwForce, ForceMode.VelocityChange);
}
 }

So it’ll throw grenade only if currentAmmo is > 0, but you already did that in the update so i don’t really understant why your script doesn’t works

you can use start and stop coroutines .