Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by Bezaleel1998, Nov 15, 2017.

  1. Bezaleel1998

    Bezaleel1998

    Joined:
    May 18, 2017
    Posts:
    22
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class coinScore : MonoBehaviour {
    8.     [Header ("ScoreManager")]
    9.     public Text scoretext;
    10.     public Text score;
    11.     private int currentScore;
    12.     public Text highScore;
    13.     private int CurrentScore
    14.     {
    15.         get { return currentScore; }
    16.         set
    17.         {
    18.             currentScore = value;
    19.             ScoreHandler();
    20.         }
    21.     }
    22.     private int min = 0;
    23.     public string HighScore;
    24.     public string minScore;
    25.  
    26.     [Space]
    27.     [Header("AudioManager")]
    28.     //inputing sound after coin was triggered
    29.     public AudioSource SoundSource;
    30.     public AudioClip Sound;
    31.     private bool hasPlayedAudio;
    32.  
    33.     void Start()
    34.     {
    35.         currentScore = min;
    36.         highScore.text = PlayerPrefs.GetInt(HighScore, 0).ToString();
    37.     }
    38.  
    39.     private void ScoreHandler()
    40.     {
    41.         scoretext.text = " " + currentScore.ToString();
    42.         score.text = " " + currentScore.ToString();
    43.  
    44.         if (currentScore > PlayerPrefs.GetInt(HighScore, 0))
    45.         {
    46.             PlayerPrefs.SetInt(HighScore, currentScore);
    47.             highScore.text = currentScore.ToString();
    48.             PlayerPrefs.SetInt(minScore, currentScore);
    49.         }
    50.         else if (currentScore < PlayerPrefs.GetInt(HighScore, 0))
    51.         {
    52.             PlayerPrefs.SetInt(minScore, currentScore);
    53.         }
    54.        
    55.     }
    56.  
    57.     void OnTriggerEnter(Collider col)
    58.     {
    59.         if (col.tag == "Coin" && hasPlayedAudio == false)
    60.         {
    61.             SoundSource.PlayOneShot(Sound);
    62.             hasPlayedAudio = false;
    63.            
    64.            Destroy(col.gameObject);
    65.            //Debug.Log("You Got The Point");
    66.            CurrentScore += 100;
    67.         }
    68.         else if (col.tag == "death")
    69.         {
    70.             CurrentScore -= 50;
    71.         }
    72.         else if (col.tag == "checkpoint")
    73.         {
    74.             CurrentScore += 150;
    75.         }
    76.         else if (col.tag == "goal")
    77.         {
    78.             CurrentScore += 200;
    79.         }
    80.     }
    81.  
    82. }
    83.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,936
    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.
     
  3. Bezaleel1998

    Bezaleel1998

    Joined:
    May 18, 2017
    Posts:
    22
    owh yeah, thanks man, but how about the Saving to the file ?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,936
    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.
     
    Bezaleel1998 likes this.
  5. Bezaleel1998

    Bezaleel1998

    Joined:
    May 18, 2017
    Posts:
    22
    thanks for the Explanation man
     
    Kurt-Dekker likes this.