Answer incorrectwhen first displaying question

The code at the end of this section didn’t work as intended- the first question displayed showed an incorrect answer - after that, it worked fine. So I copied all the code from Git Lab for this section and the same error was present. After using debug messages printing out all the boolean flags and timers etc. across the two classes I found the issue.

There is a subtle bug in the way the timerValue is initialized and then first used. timerValue is declared with no value therefore it has a default value of 0; When the updateTimer() is first called, timerValue is set -ve with the line “timerValue -= Time.deltaTime;” then isAnsweringQuestion is set false because timerValue is not >0. This causes the Quiz Update() to execute “if (!hasAnsweredEarly && !timer.isAnsweringQuestion)” with a call to DisplayAnswer(-1) and therefore displays an incorrect answer.

Sorry if the above was not clear but it is difficult to explain. Setting the timerValue to 10 seconds when it is declared solves the issue because the first time through the UpdateTimers() it doesn’t go negative.

Not sure how the GitLab code could be the final version for this section.

So the above demonstrates that the code design with many public boolean flags and timers being used as state values isn’t the best design. It served its purpose by introducing the Unity UI and interaction with buttons, canvas, images etc. but it’s not best practice and probably shouldn’t be followed for larger projects.

Hi RedSandman,

The logic in the game is simple. It compares an integer with an integer, not answers with answers or buttons with answers. Just an integer with an integer.

When you press one of the answer buttons, the answer button sends an integer to your OnAnswerSelected method. The first button should send 0, the second one 1, the third one 2, and so on.

In your QuestionSO file, you have an array with answers. The first answer is at index 0, the second answer at index 1, the third answer at index 2, and so on.

You probably recognised the pattern at this point.

In the QuestionSO file, you also defined the correct answer with an integer matching the index in your array. If the first answer is the correct answer, the value is 0.

Did this make sense so far?

If only one question does not work properly, there is very likely a problem in the corresponding QuestionSO file. Maybe the correct answer index value is wrong.

Use Debug.Log to log the respective integer values into your console to see if they are the expected values.


DisplayAnswer(-1) is supposed to show the ‘correct’ answer as defined in your QuestionSO. See these lines of code in the else-block:

correctAnswerIndex = currentQuestion.GetCorrectAnswerIndex();
string correctAnswer = currentQuestion.GetAnswer(correctAnswerIndex);
questionText.text = "Sorry, the correct answer was;\n" + correctAnswer;

And this is typical in programming: We have to distinguish between our expectation and the output. It’s not the same. ‘Correct’ in code is not necessarily ‘correct’ in our human eyes. The code might be ‘correct’ but the data might be ‘wrong’. Or the ‘data’ is correct, and there is a flaw in the logic of the code. Or the data and the code are ‘correct’ and our expectation is wrong.

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

Privacy & Terms