[C#]Trying to make a PowerUp timer.

I’m trying to use a while loop to increase the time to 15f set by one of my variables. Yet Unity keeps crashing, I’m probably just missing something small because I’m exhausted, but any help would be great! Thanks!

Here is the fragment of code including Variables.

**These are Variables**
    public float powerUpTime = 15f;
    public float powerUpDecreaseTime = 0f;
    public float powerUpIncreaseAmount = 0.05f;


**This is the Loop:**

        if (aHandle.hasScoreX2 == true || aHandle.hasScoreX3 == true || aHandle.hasScoreX5 == true)
        {
            while (powerUpDecreaseTime <= powerUpTime)
            {
                powerUpDecreaseTime = powerUpDecreaseTime + powerUpIncreaseAmount;

                if (powerUpDecreaseTime >= powerUpTime)
                {
                    if (aHandle.hasScoreX2 == true || aHandle.hasScoreX3 == true || aHandle.hasScoreX5 == true && powerUpDecreaseTime == 15f)
                    {
                        aHandle.hasScoreX2 = false;
                        aHandle.hasScoreX3 = false;
                        aHandle.hasScoreX5 = false;
                    }
                    powerUpDecreaseTime = 0f;
                }
            }
        }

Hello!

Alright, so Unity keeps crashing. Typically, I’ve found Coroutines to be the best way of making timers (they won’t crash with these kinds of things):

    // Call this with StartCoroutine(PowerUpTimer(15.0f));
    private IEnumerator PowerUpTimer(float duration) {
        float elapsedTime = 0;

        while (elapsedTime < time) {
            // Timer is currently "ticking"
            // ...

            elapsedTime += Time.deltaTime;
            yield;
        }

        // End of timer
        // ...
    }

Got it working! Thank you so much! Unity docs helped put your example into more perspective but thanks! @Stratosome