I have an issue with the final score, no matter what is the real final score value it always shows this weird number:( What might be the issue?
The score during the game looks ok.
I have an issue with the final score, no matter what is the real final score value it always shows this weird number:( What might be the issue?
The score during the game looks ok.
Hi,
There is probably an issue somewhere in your script. Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Make sure there are no typos where you are calculating the score.
Yes the scripts were compared. I copied them to double check. I guess the issue might be somewhere in my Unity game settings, is that possible?
Maybe but I have to admit that I currently do not know where you could look for the issue.
The best would be to add a Debug.Log to the code block where you calculate and assign the score. Log all the relevant values into your console to see if the score gets calculated correctly.
It might be that the problem is caused by the Text or TextMeshPro component, which might be a bug in Unity. Or it could be that the score is not calculated correctly. Or the data for the calculation is wrong. That’s impossible to tell just by looking at the rendered image.
The number showing there is the minimum value that the int
datatype can hold, so that might give you an idea of where to look.
My first guess, would be that you’re dividing by zero when calculating the score and it’s trying to return negative infinity (you can’t divide by zero and your code will do weird things if you try).
Double check where you are setting questionsSeen
and make sure the calls to increment it are working correctly.
I hope that helps.
Gary is right.
I discussed this issue with another student and recognised the “funny” value in their case. According to the C# API “[d]ividing a floating-point value by zero doesn’t throw an exception; it results in positive infinity, negative infinity, or not a number (NaN), according to the rules of IEEE 754 arithmetic”.
The problem is caused by this piece of code:
public int CalculateScore()
{
return Mathf.RoundToInt(correctAnswers / (float)questionsSeen * 100);
}
A quick fix could be an if-statement:
public int CalculateScore()
{
if (questionsSeen < 1)
{
Debug.LogWarning("questionsSeen is 0 or lower than 0.");
return 0;
}
return Mathf.RoundToInt(correctAnswers / (float)questionsSeen * 100);
}
In my example, I return 0 as the default value if questionsSeen
is 0 or lower than 0. This way, you will at least get a value that makes sense for the player. And you’ll also get a helpful message logged into your console.
Of course, when the player is playing the game, you don’t want to display 0 the entire time, so make sure that the correct value gets assigned to questionsSeen
when the game starts.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.