Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Setting high scores for multiple levels Unity

Discussion in 'Scripting' started by jbowers74321, Jan 12, 2020.

  1. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    I have been trying to get the high score's working for a while and I can't figure it out. My high scores on my shopcontrol script don't change they stay at 0, what can I do to fix this? here is how I save my score
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Net;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class Score : MonoBehaviour
    9. {
    10.     public int score;
    11.    
    12.  
    13.     private float difficultyLevel = 1;
    14.     private float maxDifficultyLevel = 10;
    15.     private float scoreToNextLevel = 10;
    16.    
    17.     private bool isDead = false;
    18.     public static int sceneName;
    19.  
    20.     public Text scoreText;
    21.     public DeathMenu deathMenu;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.        
    27.        
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         if (isDead)
    34.             return;
    35.  
    36.         if (score >= scoreToNextLevel)
    37.             LevelUp();
    38.  
    39.         score += Time.deltaTime * difficultyLevel;
    40.         scoreText.text = ((int)score).ToString ();
    41.        
    42.     }
    43.  
    44.     void LevelUp()
    45.     {
    46.         if (difficultyLevel == maxDifficultyLevel)
    47.             return;
    48.  
    49.         scoreToNextLevel *= 2;
    50.         difficultyLevel++;
    51.  
    52.         GetComponent<playermotor>().SetSpeed(difficultyLevel);
    53.     }
    54.  
    55.     public void OnDeath()
    56.     {
    57.         isDead = true;
    58.  
    59.         PlayerPrefs.SetInt("Highscore1motomadness" + sceneName, score);
    60.  
    61.         PlayerPrefs.SetInt("Highscore2Desert" + sceneName, score);
    62.  
    63.  
    64.         deathMenu.ToggleEndMenu(score);
    65.     }
    66.  
    67. }
    68.  
    and here is where I am trying to display the high scores
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. using System.Runtime.Serialization.Formatters.Binary;
    7. using System.IO;
    8. using System;
    9.  
    10. public class ShopControl : MonoBehaviour
    11. {
    12.  
    13.     public static int moneyAmount;
    14.     int isDesertSold;
    15.     public Text foresthscoreText;
    16.     public Text DeserthscoreText;
    17.     public Text moneyAmountText;
    18.     public Text desertPrice;
    19.     public static int sceneName;
    20.     public static int score;
    21.     public static int Highscore1motomadness;
    22.     public static int Highscore2Desert;
    23.     public int foresthscore;
    24.     public static int Deserthscore;
    25.  
    26.  
    27.     public Button buyButtonDesert;
    28.  
    29.     // Start is called before the first frame update
    30.     void Start()
    31.     {
    32.         moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
    33.         foresthscoreText.text = PlayerPrefs.GetInt("Highscore1motomadness" + sceneName, 0).ToString();
    34.         DeserthscoreText.text = PlayerPrefs.GetInt("Highscore2Desert" + sceneName).ToString();
    35.     }
    36.  
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.         moneyAmountText.text = "" + moneyAmount.ToString() + "¢";
    42.        
    43.  
    44.         isDesertSold = PlayerPrefs.GetInt("IsDesertSold");
    45.  
    46.         if (moneyAmount >= 5 && isDesertSold == 0)
    47.             buyButtonDesert.interactable = true;
    48.         else
    49.             buyButtonDesert.interactable = false;
    50.     }
    51. public void SetHighScore(int score)
    52.     {
    53.         if (score > PlayerPrefs.GetInt("Highscore1motomadness" + sceneName))
    54.         {
    55.             PlayerPrefs.SetInt("Highscore1motomadness" + sceneName, score);
    56.             foresthscoreText.text = score.ToString();
    57.         }
    58.  
    59.         if (score > PlayerPrefs.GetInt("Highscore2Desert" + sceneName))
    60.         {
    61.             PlayerPrefs.SetInt("Highscore2Desert" + sceneName, score);
    62.             DeserthscoreText.text = score.ToString();
    63.         }
    64.    
    65.     }
    66.  
    67.     public void GetHighScore(string sceneName)
    68.     {
    69.         PlayerPrefs.GetInt("Highscore1motomadness" + sceneName);
    70.         PlayerPrefs.GetInt("Highscore2Desert" + sceneName);
    71.     }
    72. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    You have two
    public static string sceneName;
    variables, one in each script. Are you always setting both to be equal? If not, they are different variables, so your GetInt/SetInt calls won't be talking to the same keys.
     
  3. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    Is the problem that the values aren't persisting when exiting the game?
    If so, you'll need to call PlayerPrefs.Save() - this writes all player prefs changes to disc
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    https://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html
    from the docs:
    If the game shuts down normally it's saved, you should still call save every now and then incase of a crash, but that's another problem.


    I didn't read though your code for long, but from what I can see you're setting the playerprefs variable to what you want, but you only read from it on Start()

    also, what do you think this function does?
    Code (CSharp):
    1. public void GetHighScore(string sceneName)
    2.     {
    3.         PlayerPrefs.GetInt("Highscore1motomadness" + sceneName);
    4.         PlayerPrefs.GetInt("Highscore2Desert" + sceneName);
    5.     }
    6.  
    it's just stuffing whatever is held there (be it an int or just 'null') into memory, literally not doing anything with it.
    it's like writing
    Code (CSharp):
    1. public void GetHighScore(string sceneName)
    2.     {
    3.         8;
    4.         3;
    5.     }
    6.  
     
  5. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    I have just been trying to save a high score for my motomadness level and my desert level and display it on my shop control scene using the forsesthscore text and the Deserthscore text. How can I fix it? on the shop control scene the numbers are not changing at all they are at 0. how do I set up the sceneName because if i take them off on one or both it gives me the error sceneName are you missing an assembly reference.
     
  6. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    I haven't found any video examples or anything that will help me understand using a specific scene when saving a high score and displaying it.