Search Unity

Highscore PLEASE HELP

Discussion in 'Scripting' started by Jana1108, Jul 7, 2015.

  1. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Hi, I've been searching and posting threads for weeks but I still cannot get my highscore to work. All I simply want to do is in my game over menu, if the score is larger than the best score, then set the score to the best score and save the value for when the player closes and reopens the game. Here is what I have so far but it doesn't seem to work:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Highscore : MonoBehaviour {
    6.    
    7.     public Text scoreText;
    8.     public float score;
    9.     public Text highscoreText;
    10.    
    11.     // Use this for initialization
    12.     void Start () {
    13.        
    14.         scoreText.text = "Score: " + score.ToString();
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.        
    21.         highscoreText.text = "Best: " + PlayerPrefs.GetInt("highscore");
    22.        
    23.     }
    24.    
    25.     void StoreHighscore(int newHighscore)
    26.     {
    27.         int oldHighscore = PlayerPrefs.GetInt("highscore", 0);  
    28.         if(newHighscore > oldHighscore)
    29.         {
    30.             PlayerPrefs.SetInt("highscore", newHighscore);
    31.             highscoreText = scoreText;
    32.         }
    33.     }
    34. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Where do you declare / set newHighscore? Shouldn't you be using score instead?
     
  3. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Honestly, I don't know. I'm not an experienced developer and I'm 14 years old. I can get everything else in my game working except this so I'd really appreciate someone to help me figure this out. I'd be very grateful. Are you able to take this script and edit it and then tell me exactly where I should put the script etc. At the moment I just have it in an empty game object...

    P.S. My game is an android application.
     
  4. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    I have two labels, when the scoreText.text is bigger than the highscoreText.text label, I want the highscoreText.text to be the same as scoreText.text. I also want to save the highscore for when the player exits the game they won't lose their best score.
     
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You need to compare your 2 ints & if the score is higher than the old high score then set old high score to equal the score. Then save it to the player prefs & show it on screen. The text just displays your ints so you actually set the Int value, not the text value
     
  6. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Yeah but how...?
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    In your code where you have (if newHighSxore>oldHighScore) put oldHighScore= newHighScore then only ever display the oldHighScore as the high score value. Something like that should work, or if you are only storing the high score & the players score then it will be
    If (score>high score) high score=score.
     
  8. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215