How Do I make a Temporary Power Up?

I’m making an overhead shooter game, and I made it so when the player touches a certain object, their fire rate increases. How can I make it so the power up wears off after a few seconds?

public class PlayerPickups : MonoBehaviour {

PlayerHealth playerHealth;

public float RFBoostValue=0.005f;

GameObject player;

void Awake()
   {
       player = GameObject.FindGameObjectWithTag("Player");
       playerHealth = player.GetComponent<PlayerHealth>();
   }

private void OnTriggerEnter(Collider other)
{

   if (other.tag == "Rapid Fire Pickup")
   {
       Destroy(other.gameObject);
       PlayerShooting.timeBetweenBullets -= RFBoostValue;
   }
}
}

Use a coroutine- add the effect to the character at the beginning, wait X seconds, then remove the effect before ending. You can find more information on coroutines here.

Heres’ what I tried, but the power up’s not wearing off.

   private void OnTriggerEnter(Collider other)
    { 
        
        if (other.tag == "Rapid Fire Pickup")
        {
            Destroy(other.gameObject);
            PlayerShooting.timeBetweenBullets -= RFBoostValue;           
           
                PowerUpWearOff();
           
        }
    }

    IEnumerator PowerUpWearOff()
    {
        yield return new WaitForSeconds(4);
        PlayerShooting.timeBetweenBullets += RFBoostValue;
    }

like this

public class PlayerPickups : MonoBehaviour {
    PlayerHealth playerHealth;
    public float RFBoostValue=0.005f;
    GameObject player;
    void Awake()
   {
       player = GameObject.FindGameObjectWithTag("Player");
       playerHealth = player.GetComponent<PlayerHealth>();
   }
    private void OnTriggerEnter(Collider other)
    {
   
       if (other.tag == "Rapid Fire Pickup")
       {
           Destroy(other.gameObject);
           PowerUpWearOff(5f);  //start the time function
       }
    }
  
    IEnumerator PowerUpWearOff(float waitTime)
    {
        PlayerShooting.timeBetweenBullets -= RFBoostValue; // add boost
        yield return new WaitForSeconds(waitTime);
        PlayerShooting.timeBetweenBullets += RFBoostValue; // remove boost
    }
}

I’m trying that code out, but the fire rate’s not changing. It’s like it was increased just to be decreased again.

That should be StartCoroutine(PowerUpWearOff(5f));
If you call it as a normal function, the delay won’t work.

Note that the way this is set up, multiple ones will stack, and so you could reduce your shot time to negative if you picked up enough at once.

is there anyway to set a limit? So I can’t collect two at once?

here you go

public class PlayerPickups : MonoBehaviour {
    PlayerHealth playerHealth;
    public float RFBoostValue=0.005f;
    GameObject player;
    private bool isActive = false;
   
    void Awake()
   {
       player = GameObject.FindGameObjectWithTag("Player");
       playerHealth = player.GetComponent<PlayerHealth>();
   }
    private void OnTriggerEnter(Collider other)
    {
  
       if (other.tag == "Rapid Fire Pickup" && isActive == false)
       {
            isActive = true;
           Destroy(other.gameObject);
           StartCoroutine(PowerUpWearOff(5f));  //start the time function
       }
    }
    IEnumerator PowerUpWearOff(float waitTime)
    {
        PlayerShooting.timeBetweenBullets -= RFBoostValue; // add boost
        yield return new WaitForSeconds(waitTime);
        PlayerShooting.timeBetweenBullets += RFBoostValue; // remove boost
        isActive = false;
    }
}