Changing the state - alternate if statement solution

When I’m dealing with “if statements/conditions” I like to group them and determine which one is common between operations and what I can always use as “if this is true then do this, otherwise, the only other outcome is false, so do that”, but if there’s a condition which could be complex or larger, I tend to use that one as the starter.

In this case, we’re always watching if the timer reached zero and reset the timer with another value that depends on whether “isAnsweringQuestion” is true or false. I’m not sure if this would complicate the next lecture, but here’s my solution:

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

  // This will always determine if we're reseting the timer
  if (timerValue <= 0)
  {
    // This will always determine which timer we will use next
    if (isAnsweringQuestion)
    {
      timerValue = timeToShowCorrectAnswer;
    }
    else
    {
      timerValue = timeToCompleteQuestion;
    }

    // This line is always excecuted, which is why it is outside the second
    // if statement, and by doing "= !boolVariable" it will always return the
    // opposite value of "boolVariable", in this case "isAnsweringQuestion".
    isAnsweringQuestion = !isAnsweringQuestion;
  }

  Debug.Log(timerValue);
}
2 Likes

Privacy & Terms