# How to reset the score to 0 when reloading the game?

My scoring system uses DontDestroyOnLoad to persist between scenes:

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

public void Correct(int number){
score = score + number;
}

void Start () {
score = 0;
}

void Awake() {
}

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");
}
}

• 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. – DMGregory Dec 9 '18 at 9:46

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)
{
registeredForSceneCallback = true;
}
score = 0;
}

{
score = 0;
}

...
}


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

• This answer would be better if it explained how to do that, since it seems like OP might not know. – DMGregory Dec 10 '18 at 12:15
• @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. – lentinant Dec 10 '18 at 23:32
• 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. :) – DMGregory Dec 10 '18 at 23:34