String CodeQuestionQuizMaster = "Why is this done this way?"

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

    if(isAnsweringQuestion)
    {
        
        if (timerValue <= 0)
        {
            isAnsweringQuestion = false;
            timerValue = timeToShowCorrectAnswer;

        }
    }
    else
    {
        if(timerValue <= 0)
        {
            isAnsweringQuestion = true;
            timerValue = timeToCompleteQuestion;

        }
    }

Hello!!! I’m currently doing the quiz master portion of the Unity 2d course. And tried to figure out the challenge in “part 61. Simple Timer”. When I failed time and time again with this I watched the solution (seen above) and now i’m wondering why we are using if-statements within if-statements? Why do you do it that way instead of like this?:

   if(isAnsweringQuestion && timerValue <= 0)
    {
              
       isAnsweringQuestion = false;
       timerValue = timeToShowCorrectAnswer;
        
    }
    else if (timerValue <= 0)
    {

       isAnsweringQuestion = true;
       timerValue = timeToCompleteQuestion; 
        
    }

Is there any merit to one over the other, except that the one I made is easier to look at?? Something like “If we want to do this and that in the future it is better to have a if-statement inside another if-statement” or “If you do it this way the code is more optimized for performance.”.

Regards // Gruvbyggare

No, yours is fine. In fact, you could ‘early exit’ and make it even easier to look at

void UpdateTimer()
{
    timerValue -= Time.deltaTime;
    if (timerValue > 0) return;

    if (isAnsweringQuestion)
    {
        isAnsweringQuestion = false;
        timerValue = timeToShowCorrectAnswer;
    }
    else
    {
        isAnsweringQuestion = true;
        timerValue = timeToCompleteQuestion;
    }
}

or what mine would be

void UpdateTimer()
{
    timerValue -= Time.deltaTime;
    if (timerValue > 0) return;

    if (isAnsweringQuestion)
        timerValue = timeToShowCorrectAnswer;
    else
        timerValue = timeToCompleteQuestion;

    isAnsweringQuestion = !isAnsweringQuestion;
}

That looks a little bit above what I understand at the moment since I’m still figuring out how the whole if/else/bool thing works (the neurons in my brain aren’t getting the hang of it quite yet). But I thank you for the swift and accurate answer.

I’ll explain it.

void UpdateTimer()
{
    // reduce the timer value
    timerValue -= Time.deltaTime;
    // if the timer value is still above 0, exit. we don't do anything anyway
    // if it is
    if (timerValue > 0) return;

    // if we are here it means we didn't exit, so the timer value is <= 0

    // this is the usual logic. if we were answering the question, set
    // the timer value to the 'show answer' time, else set it to the
    // 'answer question` time
    if (isAnsweringQuestion)
        timerValue = timeToShowCorrectAnswer;
    else
        timerValue = timeToCompleteQuestion;

    // toggle the flag. if it was true we set it to false, and if it was false
    // we set it to true
    isAnsweringQuestion = !isAnsweringQuestion;
}
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms