Search Unity

[HELP] Quiz Game scoring system keep revert to 0 every time the question change.

Discussion in 'Scripting' started by AmISquidward, Jun 19, 2021.

  1. AmISquidward

    AmISquidward

    Joined:
    May 20, 2021
    Posts:
    18
    Hi there... I'm making a true false quiz game following Brackey's tutorial on Youtube. I'm still very new at using unity and this is my first game. I try to add scoring system. I want the score to be add by 10 every time the player answer the right question. But the score keep going back to 0 every time it goes to the next question.

    This is how i do the code for score script. (I didn't make a code for decrease the score on purpose since i'm trying to make the game end when the player answer the question wrong.)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Scores : MonoBehaviour
    7. {
    8.     public Text scoreText;
    9.     private int _currentScore;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         _currentScore = 0;
    15.         scoreText.text = _currentScore.ToString();
    16.     }
    17.  
    18.     public void AddScore()
    19.     {
    20.         _currentScore += 10;
    21.         scoreText.text = _currentScore.ToString();
    22.     }
    23.  
    24. }
    and this is my game manager script :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class GameManager : MonoBehaviour
    9. {
    10.  
    11.     public Question[] questions;
    12.     private static List<Question> unansweredQuestions;
    13.  
    14.     public Scores scores;
    15.  
    16.     private Question currentQuestion;
    17.  
    18.     [SerializeField]
    19.     private Text factText;
    20.  
    21.     [SerializeField]
    22.     private Text trueAnswerText;
    23.     [SerializeField]
    24.     private Text falseAnswerText;
    25.  
    26.     [SerializeField]
    27.     private Animator animator;
    28.  
    29.     [SerializeField]
    30.     private float timeBetweenQuestions = 2f;
    31.  
    32.     void Start()
    33.     {
    34.         if (unansweredQuestions == null || unansweredQuestions.Count == 0)
    35.         {
    36.             unansweredQuestions = questions.ToList<Question>();
    37.         }
    38.         SetCurrentQuestion();
    39.     }
    40.  
    41.     void SetCurrentQuestion()
    42.     {
    43.         int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
    44.         currentQuestion = unansweredQuestions[randomQuestionIndex];
    45.  
    46.         factText.text = currentQuestion.fact;
    47.  
    48.         if (currentQuestion.isTrue)
    49.         {
    50.             trueAnswerText.text = "CORRECT!!";
    51.             falseAnswerText.text = "WRONG :(";
    52.         }
    53.         else
    54.         {
    55.             trueAnswerText.text = "WRONG :(";
    56.             falseAnswerText.text = "CORRECT!!";
    57.         }
    58.  
    59.     }
    60.  
    61.     IEnumerator TransitionToNextQuestion()
    62.     {
    63.         unansweredQuestions.Remove(currentQuestion);
    64.  
    65.         yield return new WaitForSeconds(timeBetweenQuestions);
    66.  
    67.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    68.     }
    69.  
    70.     public void UserSelectTrue()
    71.     {
    72.         animator.SetTrigger("True");
    73.         if (currentQuestion.isTrue)
    74.         {
    75.             Debug.Log("CORRECT!");
    76.             scores.AddScore();
    77.         }
    78.         else
    79.         {
    80.             Debug.Log("WRONG!");
    81.         }
    82.         StartCoroutine(TransitionToNextQuestion());
    83.     }
    84.  
    85.     public void UserSelectFalse()
    86.     {
    87.         animator.SetTrigger("False");
    88.         if (!currentQuestion.isTrue)
    89.         {
    90.             Debug.Log("CORRECT!");
    91.             scores.AddScore();
    92.         }
    93.         else
    94.         {
    95.             Debug.Log("WRONG!");
    96.         }
    97.         StartCoroutine(TransitionToNextQuestion());
    98.     }
    99.  
    100.  
    101. }
    After that i create score panel, and put the score text. I put the script inside the score panel, put the score text inside the script, and then put the scripted score panel inside the game manager script. When i try to run the game, it runs smoothly and when i answer the question right the score add up just like i want it to. But when it change to the next question, the score are being reset to 0. Is it because brackey's code line is so that the question get reset everytime i answer question? If thats the case, can you tech me the proper code to make the quiz keep going and the score keep adding until the the player answer a wrong question?

     

    Attached Files:

    Last edited: Jun 19, 2021
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Looks like you're reloading the scene on line 67 of your GameManager script. When you reload the scene, the objects that hold your scripts will be destroyed and recreated with their default values. This is why you're going back to zero every time you change scene.

    There are lots of options for this. You could make your class a singleton, make some fields or classes static, save the data to PlayerPrefs etc.

    With that said, I don't see the point of the scene reload on 67. Maybe you could delete this line if it's not used for anything. You'd avoid the problem entirely.
     
    AmISquidward likes this.
  3. AmISquidward

    AmISquidward

    Joined:
    May 20, 2021
    Posts:
    18
    Hi there thankyou for your reply and support. When i read your suggestion i immediately try and delete line 67, the game run well and the score add up. But it wont load to the next question. Is there any way to load the next question without restarting the whole scene?
     
  4. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    No problem :)

    So when you reload, it's resetting all the fields at the top your scripts to their default values and running Start() again on your GameManager (same on your other scripts). I assume it's just a lazy way to reset everything ready for the next question.

    Personally, I would have pulled everything out of Start() and put it into a ResetQuestions() function. Instead of changing scene 67, call ResetQuestions() instead.

    Make sure ResetQuestions() runs in Start() too.

    I'm not sure if that would have side effects elsewhere though, you'd have to test and resolve any problems as they arise.

    Is this the end of the tutorial? Before doing any of this, I wonder if later in the tutorial, he will use some way to keep the score, but he hasn't got there yet. If you're not at the end, follow it all the way through and see if he addresses this issue. I would hope he does!
     
    AmISquidward likes this.
  5. AmISquidward

    AmISquidward

    Joined:
    May 20, 2021
    Posts:
    18
    On this case, there is no more tutorial. He only thought how to make the core game (without scoring, menu, endgame, etc) and i decide to use his tutorial since its the most easier to follow and understand for newbs like me. He create a lot of tutorial of how to make the core of different game but not how to make 1 complete game. But he also make the basic tutorial like how to make main menu, how to make the game end, etc separately. Since i'm very new at this and this is my first game, i can't implement those separate tutorial into the core game tutorial that i try to develop yet :( thats why its confusing. I guess is should have follow the tutorial that make the full game instead.

    I'm not really sure if i would be able to do this or event understand how. But for now i'll try this sugestion and try your other sugestion about making a singleton class.

    But if there someone that have any other idea for me to try i still open for suggestion. Thankyou :)
     
    mopthrow likes this.