Grading system not working
Hello everyone,
I am trying to create a grading system on my game so by the end of it, depending on the score, the user gets the matching grade. Bare with as I am very new at scripting still...
using UnityEngine.UI; using UnityEngine; using System.Collections;
public class GradeScript : MonoBehaviour {
private GameController gameController;
public Text gradeText;
void Awake () {
gameController = FindObjectOfType<GameController>();
}
void Update () {
if (gameController.score > 700) {
gradeText.text = "Grade: A+";
}
if (gameController.score > 600 && gameController.score < 699) {
gradeText.text = "Grade: A";
}
if (gameController.score > 500 && gameController.score < 599) {
gradeText.text = "Grade: B";
}
if (gameController.score > 400 && gameController.score < 499) {
gradeText.text = "Grade: C";
}
if (gameController.score > 300 && gameController.score < 399) {
gradeText.text = "Grade: D";
}
if (gameController.score > 200 && gameController.score < 299) {
gradeText.text = "Grade: E";
}
if (gameController.score > 100 && gameController.score < 199) {
gradeText.text = "Grade: F";
}
}
}
This is not working for me. I am getting no errors but it's not showing up in the final scene (Grading scene). Can you please help me? Thank you.
Answer by jgodfrey · Feb 19, 2016 at 12:52 AM
I don't necessarily see anything wrong with your code, though it's a lot more verbose than necessary. The following is probably a little cleaner (though typed directly into the forum and untested).
void Update()
{
int score = gameController.score;
if (score > 700) { gradeText.text = "Grade: A+"; }
elseif (score > 600) { gradeText.text = "Grade: A"; }
elseif (score > 500) { gradeText.text = "Grade: B"; }
elseif (score > 400) { gradeText.text = "Grade: C"; }
elseif (score > 300) { gradeText.text = "Grade: D"; }
elseif (score > 200) { gradeText.text = "Grade: E"; }
else { gradeText.text = "Grade: F"; }
}
Additionally, you probably need to add some Debug.Log statements to see what's going on. For instance, is the score value what you expect it to be? Also, this seems like a strange thing to be doing in Update(). Is that really what you want?
One additional comment:
With your current, code, the following values will not match any of your checks:
600, 699, 500, 599, 400, 499, 300, 399, 200, 299, 100, 199, or anything < 100
Thank you for replying! It is still not working, and I don't understand why. I am getting the following error in the console as soon as I reach the grading scene/ win scene:
Could the problem be because I am using a script from another scene?
Ok I just figured out what is happening: If I have the game object which contains the gameController script in the scene, it is working. However, the score values are not the same. I need to somehow reference the game Object as it is in the previous scene.
Alright, never $$anonymous$$d. I found a different way into it. Ins$$anonymous$$d of changing the scene, I'm having a panel pop up showing the grade and it's working perfectly. Again, thanks for aaaall your help!
Your answer
Follow this Question
Related Questions
Score counter not working properly. 1 Answer
I need help with my script please 1 Answer
The laser problems 0 Answers