Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Score System problems.

Discussion in 'Scripting' started by TechHead404, May 10, 2019.

  1. TechHead404

    TechHead404

    Joined:
    Feb 25, 2019
    Posts:
    13
    Right now the game keeps high score but its the same score for all the levels. I have 4 levels and I want to have high scores for each level. I also want to have a total score. Problem is I'm learning C# and Unity while making the game. So to sum it up I have NO IDEA what I am doing. Any help would be great.

    GameManager Script:

    Code (CSharp):
    1.   void OnPlayerScored()
    2.     {
    3.         score++;
    4.         scoreText.text = score.ToString();
    5.        
    6.     }
    7.  
    8.     void OnPlayerDied()
    9.     {
    10.         gameOver = true;
    11.         int savedScore = PlayerPrefs.GetInt("HighScore" );
    12.         if (score > savedScore)
    13.         {
    14.             PlayerPrefs.SetInt("HighScore" , score);
    15.         }
    16.         SetPageState(PageState.GameOver);
    17.     }
    HighScore Script:
    Code (CSharp):
    1.  
    2.  
    3. public class HighscoreText : MonoBehaviour
    4. {
    5.    
    6.     Text score;
    7.  
    8.     void OnEnable()
    9.     {
    10.         score = GetComponent<Text>();
    11.         score.text = "High Score: " + PlayerPrefs.GetInt("HighScore").ToString();
    12.     }
    13. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,147
    Well you've already got everything you need, you just need to make a unique key per level.

    Instead of PlayerPrefs.GetInt("HighScore"), do:
    Code (csharp):
    1.  
    2. PlayerPrefs.GetInt("HighScore_" + level);
    3.  
    ... and same with SetInt().

    To tally them all up:
    Code (csharp):
    1.  
    2. int totalScore = 0;
    3.  
    4. for(int i = 0; i < total number of levels; ++i)
    5.      totalScore += PlayerPrefs.GetInt("HighScore_" + i);
    6.  
    7. Debug.Log("Player's score for all levels: " + totalScore);
    8.  
     
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    603
    You save highscore into player prefs under label "Highscore". For every other level. Of course they override each other every time. What you need is to give them different names when saving and loading. It may look like this:

    Code (CSharp):
    1. void OnPlayerDied()
    2.     {
    3.         var highscoreLabel = "HighScore_" + gameObject.scene.name;
    4.         gameOver = true;
    5.         int savedScore = PlayerPrefs.GetInt(highscoreLabel);
    6.         if (score > savedScore)
    7.         {
    8.             PlayerPrefs.SetInt(highscoreLabel, score);
    9.         }
    10.         SetPageState(PageState.GameOver);
    11.     }
    Here I've just append scene name to the label, so for every scene the label will be different, like "Highscore_Level_1" if you scene named "Level_1".
     
    GroZZleR likes this.
  4. TechHead404

    TechHead404

    Joined:
    Feb 25, 2019
    Posts:
    13
    I tried that last night but it didn't work. But it works now so..... Thanks for the help. Now just to fix the other million problems and bugs.