0

I have created this endless runner where the player have to dodge obstacles for earning points and for scoring system I have create a score manager script which is placed on back of the screen . I am able to display my score on the main screen using UI. but I want to display the score on a game over scene and also create a high score system on the game over scene.Also created public static to save the data even though it saved my data and displayed it on the game over scene but when I restarted my game the score did not start at 0 but it was the continuation of the last game score. please help me HERE IS THE SCORE MANAGER SCRIPT OF MY GAME

using UnityEngine;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour
{
    public static int score = 0;
    public Text scoreDisplay;

    public void Update()
    {
        scoreDisplay.text = score.ToString();
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.CompareTag("Obstacle")) 
        {
            score++;
            Debug.Log(score);
        }
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Please use the correct tags. `unityscript` is/was a custom JavaScript flavor like language used in previous Unity versions and is long deprecated by now. Your code is in `c#`! – derHugo Oct 20 '20 at 09:35

1 Answers1

0

You should probably use two separate variables for score and best score. The score variable is the one that it resets when you start a new game and best score should save itself between game instances. For saving an int variable between game instance you should use PlayerPrefs. The code should look like this:

using UnityEngine;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour
{
    //You don't need static for this variable
    public int score = 0;
    public Text scoreDisplay;

    public void Update()
    {
        scoreDisplay.text = score.ToString();
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.CompareTag("Obstacle")) 
        {
            score++;
            if(score > PlayerPrefs.GetInt("BestScore"))
                PlayerPrefs.SetInt("BestScore", score);
            Debug.Log(score);
        }
    }
}

Now every time you need to retrieve your best score you simply need to call:

PlayerPrefs.GetInt("BestScore")

Since "GetInt" is a static method you are able to retrieve the value of that variable no matter the scope.

PS. I'm not sure if you are aware of what "Static" really means, so you should take a look over the documentation.

  • thank you so much for this . can u also help me fixing my 1st problem of displaying the score on a different scene ?? again tysm – Harsh Mehta Oct 20 '20 at 13:21
  • Hello, to be able to access variables across different scripts instances that are placed in different scenes you could simply use "PlayerPrefs" class. Now I understand why you would want to use static score variable, but personally I don't think is a good solution. PlayerPrefs is the way to go, and if you want more functionality, take a look at Unity's approach to singletons. Hope this helps! – Comaneanu Mugurel Oct 22 '20 at 08:12