Countdown Timer C#

Hi,

I’ve got a timer in my game which counts down from 5 seconds. The problem I have is that I want to reduce the timer by 2 % each time it is reset and I’m struggling with a way to do this.

The reason I’m finding it difficult is that I want to display the countdown to the user as it happens, otherwise I would probably use a Coroutine and just have a function where I times the timer value by 0.98 to reduce it by 2% and use WaitForSeconds(value);

As it stands, I am counting down my value like this.

void Update(){
BeginCountdown();
}

void BeginCountdown(){
timerValue -= Time.deltaTime;
if (timerValue <= 0){
//Do something
}
}

This enables me to just use the timerValue in a UI text element to display to the user. However I’m changing the value with each update, so to reset I have to manually reset it back to 5 after the function ends.

I want to reset it back to a new value of 5 less 2%. I’m sure the solution is staring right at me, but i’ve been overthinking this for over an hour and my head is blagged now haha.

Does anyone have any ideas?

Store the original maximum, and store the percentage of decrease. Then, when you want to reset the timer, you can calculate what the new maximum value is, and start your countdown from that.

Thanks kru,

I’ve done what you said and Im using 2 variables now,a start and current value, I just manually adjust my start variable back to 5 when a new game is called.

Also displaying the countDown to the user doesn’t mean you can’t use a coRoutine. Coroutines are just special Updates as far as your code is concerned. But a coroutine seems like overkill since you’d just be yielding every frame, so that implies you should just do it in Update. Here is a script that assumes you have a UI Text box somewhere to display the time. You can other gameObjects call StartTimer or ResetTimer

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class DegradingTimer : MonoBehaviour {
    public float maxTimer = 10f;
    public float degradePerc = .02f;
    public Text timerText;

    private float currentMaxTime;
    private float currentTime;
    private bool isCountingDown;
 
    void Awake()
    {
        currentMaxTime = maxTimer;
        isCountingDown = false;
    }

    void Update()
    {
        if (isCountingDown)
            UpdateCounter();
    }
 
    public void StartTimer()
    {
        currentTime = currentMaxTime;
        isCountingDown = true;
        DisplayTime();
    }

    public void ResetTimer()
    {
        currentMaxTime -= maxTimer * degradePerc;
        StartTimer();
    }

    void UpdateCounter()
    {
        currentTime -= Time.deltaTime;
        if (currentTime < 0)
        {
            currentTime = 0;
            isCountingDown = false;
        }
        DisplayTime();
    }

    void DisplayTime()
    {
        timerText.text = currentTime.ToString() + "s";
    }
}

You can adjust DisplayTime to be whatever you need it to be.

Thanks takatok

That script looks like exactly what I need, I will update my own code and use this as reference. The code above looks much cleaner. Thanks again to both