1
\$\begingroup\$

My scoring system uses DontDestroyOnLoad to persist between scenes:

public class ScoringSystem : MonoBehaviour {
 private int score = 0;

 public void Correct(int number){
     // If correct add score;
     score = score + number;
 }

 void Start () {
     score = 0;
 }

 void Awake() {
   DontDestroyOnLoad(transform.gameObject);
 }

 public int GetScore(){
     return score;
 }
}

Here is another script that I should include a description about:

  public void iniTag()
 {
     jawab = true;
     Time.timeScale = 0;

         GameObject objek1 = GameObject.FindGameObjectWithTag("Benar1");
         GameObject objek2 = GameObject.FindGameObjectWithTag("Benar2");
         GameObject objek3 = GameObject.FindGameObjectWithTag("Benar3");
         GameObject objek4 = GameObject.FindGameObjectWithTag("Benar4");

         if (objek1.transform.parent.tag == "Jawab4" && objek2.transform.parent.tag == "Jawab3" &&
             objek3.transform.parent.tag == "Jawab2" && objek4.transform.parent.tag == "Jawab1")
         {
             PanelBenar.SetActive(true);
             buttonNext.SetActive(true);
             GameObject.Find("QuizManager").SendMessage("Correct", playerScore);

     }}

     void Start () {
       if (GameObject.Find ("QuizManager") != null) {
         ScoringSystem scoringSystem = GameObject.Find ("QuizManager").GetComponent<ScoringSystem> ();
         gameObject.GetComponent<Text>().text = scoringSystem.GetScore().ToString ();
             } else {
         Debug.Log("No Scoring system found. To test scoring system start from the front page scene");            
       }
     }
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Hi riska anisah, I've fixed some formatting in your question. To be able to answer you, we need to understand what your game is doing right now. So far, you haven't told us anything about what this second script does, or even what it's called, and haven't shown us how you reload the game / when you want the score to reset to 0. It's unclear why you're using DontDestroyOnLoad for your scoring system if you don't want it to be persistent. Please edit your question to clarify. \$\endgroup\$
    – DMGregory
    Dec 9, 2018 at 9:46

2 Answers 2

3
\$\begingroup\$

Here's what you want to do, most likely ('...' means omitted code):

using UnityEngine.SceneManagement;

public class ScoringSystem : MonoBehaviour {
 ...

 bool registeredForSceneCallback = false;

 ...

 void Start ()
 {
     if (!registeredForSceneCallback)
     {
         SceneManager.sceneLoaded += OnSceneLoaded;
         registeredForSceneCallback = true;
     }
     score = 0;
 }

 void OnSceneLoaded(Scene scene, LoadSceneMode mode)
 {
    score = 0;
 }

 ...
}

Additionally, inside the OnSceneLoaded method you might want to check that the scene.name value matches the scene that you're reloading on restart.

For more info on how to receive callbacks from SceneManager take a look at: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html

\$\endgroup\$
2
\$\begingroup\$

I believe, simplest way to do this is manually reset score when you actually restart the game.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ This answer would be better if it explained how to do that, since it seems like OP might not know. \$\endgroup\$
    – DMGregory
    Dec 10, 2018 at 12:15
  • \$\begingroup\$ @DMGregory given info from answer, I believe OP already has logic for restart. Also, I think OP would know how to change variable of one script from another, as it is kinda trivial. \$\endgroup\$
    – lentinant
    Dec 10, 2018 at 23:32
  • \$\begingroup\$ You might be surprised how many times we've had thar "trivial" question asked here. ;) Remember we get a lot of traffic from beginners who would benefit from a more in-depth explanation when you can — and will often reward a more descriptive answer with upvotes. :) \$\endgroup\$
    – DMGregory
    Dec 10, 2018 at 23:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .