If Statement for timer

Hello,

Is this answer correct too ?

void UpdateTimer()
    {
        timerValue -= Time.deltaTime;

        if(timerValue <= 0 && isAnsweringQuestion == false)
        {
            timerValue = timeToCompleteQuestion;
            isAnsweringQuestion = true;
        }
        if(timerValue <= 0 && isAnsweringQuestion == true)
        {
            timerValue = timeToShowCorrectAnswer;
            isAnsweringQuestion = false;
        }
1 Like

Hi,

In many cases, there are multiple ways to make something work in Unity, and Rick cannot show all of them. If your code works, it’s a solution by definition. :slight_smile:

Is there a reason why you want to check both conditions if the first one got evaluated to true? If isAnweringQuestion gets set to true in the first if-block, the second if-condition gets evaluated to true, and the second if-block gets executed.

[Edit:] Bixarrio is right. timerValue is probably not 0 the second time but “else if” would be better anyway.


See also:

3 Likes

I was going to say this, but in this specific case, timeToCompleteQuestion would have to be 0 (or less) for it to be an issue. So, in this scenario it should be fine - you’d want to give the player some time to complete the question :grin: but there certainly are situations where this could cause many hours of headaches before it is discovered

2 Likes

Ah, you are right. I missed the part where timerValue gets set to timeToCompleteQuestion, which is probably not 0.

1 Like

Privacy & Terms