Getting a strange score

My quiz has 5 questions. If I get 4/5 correct I would expect the score to be 80% but the calculation is showing 85%.

The relevant code:

public void OnAnswerSelected(int index)
{
    hasAnsweredEarly = true;
    DisplayAnswer(index);
    SetButtonState(false);
    timer.CancelTimer();
    scoreText.text = "Score: " + scoreKeeper.CalculateScore() + "%";
   

    if (progressBar.value == progressBar.maxValue)
    {
        isComplete = true;
    }

}

 public int CalculateScore()
 {
     return Mathf.RoundToInt( correctAnswers / (float)questionsSeen * 100);
     
 }

I’ve checked the correctAnswers and questionsSeen values at the end and they are showing as 4 and 5.

Update: The end screen is actually showing 85% regardless of my true score.

Any thoughts?

Kevin

Hi Kevin,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? If not, do that please. Maybe correctAnswers or questionsSeen do not get set correctly at runtime.

Yes, I set a debug log and (as stated) if I get 4 /5 correct, they show 4 and 5.

Something seems to be happening on the end screen - the scores during the game are correct.

public void ShowFinalScore()
 {
     finalScoreText.text = "Congratulations!\nYou got a score of " +
                            scoreKeeper.CalculateScore() + "%" ;

 }

Kevin

Are there any error messages in your console? Maybe a NullReferenceException? Or does the endScreen not get the current values?

Is the problem with 85 % limited to the end screen or is that also a problem elsewhere in the game?

Here’s a screenshot of the FinalScoreText element:

When we set it up we are asked to supply some dummy wording - that’s where the 85% is coming from. The code isn’t overwriting this.

How might I fix this? [I must have missed this part of the lecture?]

K

That’s important information. :slight_smile:

The reason for this behaviour is very likely that finalScoreText either does not reference the TMP component, or ShowFinalScore never gets called.

As a side note, please do not use misleading dummy text. Your dummy text looks as if it could be the actual text, which makes debugging difficult. For future reference, use something that’s definitely ‘wrong’ at runtime. An example would be ### or lorem ipsum. Your dummy text must immediately show you that it is a dummy text. Otherwise, you might waste your time in future projects looking for the problem in the wrong place.

Good advice re the dummy text - thanks.

I got the score showing - I hadn’t run the ShowFinalScore method.

Thanks,

Kevin

Does that mean the problem is solved? :slight_smile:


See also:

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

Privacy & Terms