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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to save highscore? ;_;

Discussion in 'Scripting' started by Deleted User, Sep 18, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hello, i cannot get working high score saving script ;_; Please help!
    This is my Score.cs:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Score : MonoBehaviour {
    6.     private Player _playerHandle;
    7.     public int highScore = 0;
    8.     public Text scoreText;
    9.     public Text highScoreText;
    10.  
    11.     void Start (){
    12.         _playerHandle = GameObject.Find ("Player").GetComponent<Player> ();
    13.     }
    14.  
    15.     void Update (){
    16.         scoreText.text = "Score: " +_playerHandle.score;
    17.         highScoreText.text = "High Score: " + PlayerPrefs.GetInt("highScore");
    18.     }
    19.  
    20.     void StoreHighscore(int highScore){
    21.         if (_playerHandle.score > PlayerPrefs.GetInt("highScore")) {
    22.             highScore = _playerHandle.score;
    23.             PlayerPrefs.SetInt("highScore", _playerHandle.score);
    24.         }
    25.     }
    26. }
    In player script i have score int 0, it's there bcz of detecting trigger enter to count score. What i'm doing wrong? High score just stays at 0 all the time!
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    The idea of PlayerPrefs is not to obtain them every frame I think. It's made for storage in between sessions. But, you might want to save them after setting them.
    Code (csharp):
    1.  
    2. PlayerPrefs.Save();
    3.  
     
  3. Deleted User

    Deleted User

    Guest

    Still shows 0 ;l
     
  4. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Score : MonoBehaviour {
    6.  
    7. void Update () {
    8. if (Input.GetKeyDown("s")) {SaveHighscore();}
    9. if (Input.GetKeyDown("u")) {SetHighscoreUI();}
    10. if (Input.GetKeyDown(KeyCode.UpArrow)) {IncreaseHighscore();}
    11. }
    12.  
    13. public GameObject HighscoreText;
    14. int Highscore = 0;
    15.  
    16. public void SetHighscoreUI () {
    17.     if (PlayerPrefs.HasKey("highscore")) {
    18.         HighscoreText.GetComponent<Text>().text = PlayerPrefs.GetInt("highscore").ToString();
    19.     }
    20. }
    21. public void SaveHighscore () {PlayerPrefs.SetInt("highscore",Highscore);}
    22. public void IncreaseHighscore () {Highscore++;}
    23.  
    24. }
    25.  
    Does this work for you?
     
  5. Deleted User

    Deleted User

    Guest

    I guess no. ;l
     
  6. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    This should work without the [*] and stuff with list

    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]using UnityEngine;
    4. [*]using System.Collections;
    5. [*]using UnityEngine.UI;
    6. [*]
    7.  
    8. [*]public class Score : MonoBehaviour {
    9. public GameObject HighscoreText;
    10. [/LIST]
    11. [LIST][*]      public int Highscore = 0;[/LIST]
    12.  
    13. [LIST=1]
    14. [*]void Update () {
    15. [*]if (Input.GetKeyDown("s")) {
    16. SaveHighscore();
    17. }
    18. [*]if (Input.GetKeyDown("u")) {
    19. SetHighscoreUI();
    20. }
    21. [*]if (Input.GetKeyDown(KeyCode.UpArrow)) {
    22. IncreaseHighscore();
    23.       }
    24. [*]}
    25. [*]public void SetHighscoreUI () {
    26. [*]        HighscoreText.text = PlayerPrefs.GetInt("highscore").ToString();
    27. [*]}
    28.  
    29.  
    30. [*]public void SaveHighscore () {
    31. PlayerPrefs.SetInt("highscore", Highscore);
    32. }
    33.  
    34.  
    35. [*]public void IncreaseHighscore () {
    36.       Highscore += 1 * Time.DeltaTime;
    37.       }
    38. [*]}
    39. [*]
    40. [/LIST]
    41.  
     
  7. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Looking at OPs original script makes me wonder why do you need highScore variable inside of ScoreHighscore() function. But otherwise the script should work.
    Where do you call ScoreHighscore function? Have you tried adding Debug.Log into the function to make sure it actually runs?