how to make the different High Score Every Level with C#?

Hello Guys,

I need some help from you (especially Master). i want to make the High Score at every level looks different, i was try that but it have long script for it. are it have the simple way to this script ?
thank’s for your attention.

this is my script called coinScore.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class coinScore : MonoBehaviour {
    [Header ("ScoreManager")]
    public Text scoretext;
    public Text score;
    private int currentScore;
    public Text highScore;
    private int CurrentScore
    {
        get { return currentScore; }
        set
        {
            currentScore = value;
            ScoreHandler();
        }
    }
    private int min = 0;
    public string HighScore;
    public string minScore;

    [Space]
    [Header("AudioManager")]
    //inputing sound after coin was triggered
    public AudioSource SoundSource;
    public AudioClip Sound;
    private bool hasPlayedAudio;

    void Start()
    {
        currentScore = min;
        highScore.text = PlayerPrefs.GetInt(HighScore, 0).ToString();
    }

    private void ScoreHandler()
    {
        scoretext.text = " " + currentScore.ToString();
        score.text = " " + currentScore.ToString();

        if (currentScore > PlayerPrefs.GetInt(HighScore, 0))
        {
            PlayerPrefs.SetInt(HighScore, currentScore);
            highScore.text = currentScore.ToString();
            PlayerPrefs.SetInt(minScore, currentScore);
        }
        else if (currentScore < PlayerPrefs.GetInt(HighScore, 0))
        {
            PlayerPrefs.SetInt(minScore, currentScore);
        }
       
    }

    void OnTriggerEnter(Collider col)
    {
        if (col.tag == "Coin" && hasPlayedAudio == false)
        {
            SoundSource.PlayOneShot(Sound);
            hasPlayedAudio = false;
           
           Destroy(col.gameObject);
           //Debug.Log("You Got The Point");
           CurrentScore += 100;
        }
        else if (col.tag == "death")
        {
            CurrentScore -= 50;
        }
        else if (col.tag == "checkpoint")
        {
            CurrentScore += 150;
        }
        else if (col.tag == "goal")
        {
            CurrentScore += 200;
        }
    }

}

In the script above, the public field “HighScore” contains the PlayerPrefs key used to store the score. If you make that value different on each level (at the prefab or scene level), then you will have separate high scores automatically.

owh yeah, thanks man, but how about the Saving to the file ?

There is no file code in your sample code. It uses PlayerPrefs which is a shared key/value pair store that Unity provides. It is ultimately backed up by some kind of file but you don’t have to worry about it.

If you want your own actual high score file, there are plenty of examples of using the filesystem on the web. It’s just basic C# filesystem code to do it, but the trick is making it platform dependent, and Unity provides a host of ways to do this, all of which you can find examples for online.

1 Like

thanks for the Explanation man

1 Like