Trying to add a "Charges system" to my boost mechanic

Good morning everyone, ok so long story short:
I have this “Boost” IEnumerator that works just fine, only problem is that I’d like to add a “Charges system” that starts at 3 and goes down by 1 every time the player boosts and then automatically refills after a short period of time. The code is:

IEnumerator Boost ()
	{

		if (BoostTimes != 0) {
			if (Input.GetButtonDown ("Fire3")) {
				BoostTimes--;
				speed += BoostSpeed;
				yield return new WaitForSeconds (BoostDuration);
				speed -= BoostSpeed;
				Debug.Log (BoostTimes);
			} 
			else if (BoostTimes==0)
			{
				yield return new WaitForSeconds (BoostRecharge);
				BoostTimes+=3;
				Debug.Log (BoostTimes);
			}
		}
	}

For some weird reason the “Else if” statement doesn’t get called at all. Without the else if statement the BoostTimes variable refills, but it remains at 3 forever. Any tips?

Edit: Updated code example.

Looks like you’ve nested your if statements incorrectly.

IEnumerator Boost ()
     {
 
         if (BoostTimes > 0) {
             if (Input.GetButtonDown ("Fire3")) {
                 BoostTimes--;
                 speed += BoostSpeed;
                 yield return new WaitForSeconds (BoostDuration);
                 speed -= BoostSpeed;
                 Debug.Log (BoostTimes);
             } 
         }
         else
         {
                 yield return new WaitForSeconds (BoostRecharge);
                 BoostTimes+=3;
                 Debug.Log (BoostTimes);
         }
     }

But I’m not sure your coroutine will work correctly like that. You should move the input poll to your update function (or wherever you are polling for input). Something like

  bool canBoost;
        
        void Update()
        {
            if(canBoost)
            {
               if (Input.GetButtonDown ("Fire3")) {
                   StartCoroutine(Boost());
                }
            }
        }
        
        
        IEnumerator Boost ()
        {
             canBoost = false;
             BoostTimes--;
             speed += BoostSpeed;
             yield return new WaitForSeconds (BoostDuration);
             speed -= BoostSpeed;
             Debug.Log (BoostTimes);
           
            if(BoostTimes <= 0)
            { 
               yield return new WaitForSeconds (BoostRecharge);
               BoostTimes = 3;
               Debug.Log (BoostTimes);
               canBoost = true;
             }
            else
            {
              canBoost = true;
             }
        }