Quiz-Game - Scoring

I just finished the Quiz Game module.

I am having a hard time having the score to set to 0% when we start the game…

I also noticed if we fail to make a selection in time our score will not update. I’m also having a hard time figuring out how to do this as well…

I thought about putting the .CalculateScore(); method in the timer script but then thought that might not be the right approach.

Has anyone been able to solve these issues?

Hi,

Welcome to our community! :slight_smile:

What value do you see when the game starts? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Hi Nina,

I see a score of 100 as the first score (because this is the dummy text)

Even on the course ending video you can see his score starts at 100 on launch as well. It will only update to 50% after answering the first question.

Is there any way to run calculate score on run?

No I haven’t tried adding debug.Logs I’m not really sure where it should go?

If you take a look at the ScoreKeeper class, you’ll see that Gary’s initial values are 0. What you could try is to add a Debug log to the CalculateScore method.

public int CalculateScore()
{
    int score = Mathf.RoundToInt(correctAnswers / (float)questionsSeen * 100);
    Debug.Log(Time.frameCount + " --- Score: " + score);

    return score;
}

In frame 0, the score should be 0 if correctAnswers and questionsSeen are both 0.

Even on the course ending video you can see his score starts at 100 on launch as well. It will only update to 50% after answering the first question.

Try to reenact Gary’s logic to see if his score keeper does what you think it does. We are not calculating the score based on all available questions but on the questions that have already been seen.

These three methods get called in the Quiz object:

scoreKeeper.IncrementQuestionsSeen();
scoreKeeper.IncrementCorrectAnswers();
scoreKeeper.CalculateScore();

Make sure they are getting called in the correct order. Debug.Logs with meaningful messages might help.

Remember you can also look at the lecture code changes via the link in the Resources of each lecture.

Hi Nina,

Yes, my initial values are also:
int correctAnswers = 0;

int questionsSeen = 0;

But the dummy text we put in the Score TextmeshPro box is set to “Score: 100%”

This text is only ever overwritten when we run:
scoreText.text = "Score: " + scoreKeeper.CalculateScore() + “%”;

But these only run when we make a selection.

Even your suggested Debug.Log:

Blockquote
public int CalculateScore()
{
int score = Mathf.RoundToInt(correctAnswers / (float)questionsSeen * 100);
Debug.Log(Time.frameCount + " — Score: " + score);

return score;

}

Would not run if we don’t make any selections during the game.

I.e. we do not have a method to calculate score if we run out of time for example.

So theoretically we can start the game, make no selection the entire game, and it will have “Score: 100%” being displayed the entire time.

Then when the game ends we run:
endScreen.ShowFinalScore();

Which will finally calculate the score to be 0%.

If we wanted to calculate score when the timer runs out would we put
scoreText.text = "Score: " + scoreKeeper.CalculateScore() + “%”;

In the else block of the DisplayAnswer(int index) method?

I just tried it out and it looks like it will calculate as expected if we put:
scoreText.text = "Score: " + scoreKeeper.CalculateScore() + “%”;

In the else block of the DisplayAnswer(int index) method.

But I also tried to put:
scoreText.text = "Score: " + scoreKeeper.CalculateScore() + “%”;

In the Awake() and added in the Start() method and both methods give us

Score: -2417483648% when we try this… not sure why.

I added:
Debug.Log("Questions seen: " + scoreKeeper.GetQuestionsSeen());

Debug.Log("Correct Answers: " + scoreKeeper.GetCorrectAnswers());

To see if I could figure out how we’re getting such a weird score off the bat but it doesn’t seem to make sense.

The values I get are:
Questions see: 0,

Correct Answers: 0

Score: -2417483648%

Good job so far. That’s the value that gets returned when the code divides a float by 0. The solution is simple, so maybe you would try to fix the problem yourself?

Click here if you want to take a look at my solution:
public int CalculateScore()
{
    if (questionsSeen == 0) { return 0; }

    return Mathf.RoundToInt(correctAnswers / (float)questionsSeen * 100);
}

See also:

So I kind of figured out the if you dont answer in time alth0ough it has caused another issue, in that if you don’t answer in time the buttons remain active and I cant figure that part out.

This is my update loops for the Quiz script. I just added && timer.Equals(0) but I;m not sure where to go from the there

    void Update()
    {
        
            timerImage.fillAmount = timer.fillFraction; 
            if(timer.loadNextQuestion)
            {
                if(progressBar.value == progressBar.maxValue)
                 {
                    isComplete = true;
                    return;
                  }
                hasAnsweredEarly = false;
                SetButtonState(false);
                GetNextQuestion();
                timer.loadNextQuestion = false;
                
            }
            else if(!hasAnsweredEarly && !timer.isAnsweringQuestion && timer.Equals(0))
            {
                SetButtonState(false);
                DisplayAnswer(-1);       
                
            }

    }

I just added this to the Awake function in the quiz script and I’m starting at 0%, to be honest I typed score.sco___ until the autofill gave me some ideas!

scoreText.text = "Score\n " + "%" + score.GetCorrectScore();

Hi s_kyle,

Good job on trying to solve this problem yourself. :slight_smile:

What is/was your idea behind timer.Equals(0)? Did this part solve a problem for you? If so, which one?

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

Privacy & Terms