K
K
K1ald2021-08-27 14:17:55
C++ / C#
K1ald, 2021-08-27 14:17:55

How to reset the record when moving to another scene?

[SerializeField] Text HighScoreText;
    [SerializeField] Text scoreText;
    public int score;
    [SerializeField] int highScore = 0;

    public bool canPickScore = true;

    void Start()
    {
        //score = 0;
        loadScores();
    }
    void Update()
    {
        HighScoreText.text = "Лучший счет: " + highScore.ToString();

        if (score > highScore)
        {
            highScore = score;
            saveScores();
        }

    }

    public void saveScores()
    {
        PlayerPrefs.SetInt("highScore", highScore);
        PlayerPrefs.Save();
    }

    public void loadScores()
    {
        highScore = PlayerPrefs.GetInt("highScore");
    }

    private void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "ScoreTrigger" && canPickScore == true)
        {
            score++;
            scoreText.text = score.ToString();
            saveScores();

        }
    }

This is the code for the score and record with the PlayerPrefs saved ...

But you need to make sure that each level has its own record counter.

Need to make "Best Score/Record" in a game with multiple levels.
I did this, but the problem is that the record is saved when moving to the next level, but you need to make sure that it is reset.
Otherwise, it will turn out that the player can "make a high score" on an easy level, then move on to the next ones and it will seem that the player has scored a high score on a difficult level, although in fact it is not.
Any ideas on how to implement this correctly?

I have one... When moving from scene to scene, reset the variable, but I have no idea how to do it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura Mailler, 2021-08-27
@Yura111

I did this, but the problem is that the record is saved when moving to the next level

As for me, it is logical that he will keep the score. you have PlayerPrefs, first reset the score, and then save, if you need exactly the best score on the level and the total, then just make two PlayerPrefs, one will be the score on the level that will be updated and the other best score, which will be equal to the score of the level when record .
I have one... When moving from scene to scene, reset the variable, but I have no idea how to do it.

in Awake you make the score zero

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question