• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by BrodyDaChappy · Feb 05, 2016 at 07:41 PM · scorehighscoreshighscore

Highscore issues

HI All,

My high score is not saving on death, i need it to save the score if it is higher than the current highscore and then keep it that score until a higher level is obtained, any ideas why this wont work?

Below is my level manager script.

using UnityEngine; using System.Collections;

public class LevelManager : MonoBehaviour {

 public LevelManager levelManager;
 public int score;
 public int currentLevel;
 public int Score=30;
 int highScore;
 

 private PlayerController player;

 // Use this for initialization
 void Start () {
     player = FindObjectOfType<PlayerController>();
     if (Application.loadedLevelName == "Level_1") {
         score = 0;
         saveScores();
     } else {
         score = getSavedScores();
         highScore = score;
     }  

 }

// Update is called once per frame void Update ()

 {  
     OnGUI2D.OG2D.CheckHighScore();
     saveScores();
     changeLevel();
 }
 
 public void changeLevel()
 {
     if (score == 750 && currentLevel == 1)
     {

         saveScores();
         currentLevel++;
         OnGUI2D.OG2D.CheckHighScore();
         highScore = score;
         Application.LoadLevel("2");

     }    
     if(Application.loadedLevelName == "Level_2")
     {
         getSavedScores();    
     }

     if (score == 1530 && currentLevel == 2)
     {
         saveScores();
         currentLevel++;
         OnGUI2D.OG2D.CheckHighScore();
         highScore = score;
         Application.LoadLevel("3");
         
     }    
     if(Application.loadedLevelName == "Level_3")
     {
         getSavedScores();    
     }
     
 }
 //possible if the save functionality intreacts with this
 public void saveScores()
 {
     if (score >= highScore)
     {
         highScore = score;
         PlayerPrefs.SetInt("HighScore",highScore);
         
     }
 }
 public int getSavedScores()
 {
     
     int tempScore = PlayerPrefs.GetInt("Score");  
     
     return tempScore;
 }
 
 
 public  int getScore()
 {    
     
     
     return score;
 }
 public void AddPoints(int points)
 {
     score += points;
 }

 void OnGUI()
 {
     GUI.Label(new Rect(10,10,100,20),"Score: " + score);
 }

 void OnTriggerEnter2D(Collider2D other)
 {
     if(other.name == "Player")    

     {
         OnGUI2D.OG2D.CheckHighScore();
         Application.LoadLevel(0); //this is on death completely restart game
     }


 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BrodyDaChappy · Feb 05, 2016 at 07:41 PM 0
Share

OnGUI2D Script..

using UnityEngine; using System.Collections;

public class OnGUI2D : $$anonymous$$onoBehaviour {

 public static OnGUI2D OG2D;

 public static int score;
 int highScore;
 
 // Use this for initialization
 void Start () 
 {
     OG2D = this;
     score = 0;
     highScore = PlayerPrefs.GetInt("HighScore",0);
 }
 
 void OnGUI()
 {
     GUI.Label(new Rect(10,10,100,20),"Score: " + score);
     GUI.Label(new Rect(10,30,100,20),"HighScore: " + highScore);
 }

 public void CheckHighScore()
 {
     if(score >= highScore)
     {
         highScore = score;
         Debug.Log ("Saving Score");
         PlayerPrefs.SetInt("HighScore",highScore);

     }
 }
 

}

avatar image LazyElephant · Feb 06, 2016 at 01:11 AM 0
Share

It looks like you're not actually assigning the saved score to a variable after you load it from player preferences at lines 21 and 35.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Light997 · Feb 06, 2016 at 11:06 AM

The two scripts:

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using System.Collections.Generic;
 
 public class LevelManager : MonoBehaviour
 {
     // I didn't quite understand what this reference was good for, so I made a Singleton pattern out of it
     // the instance variable is static, so you can now call on the LevelManager without a reference with LevelManager.instance
     #region Singleton Pattern
     /// <summary>
     /// A static instance of the LevelManager. Accessible from anywhere in the scene.
     /// </summary>
     public static LevelManager instance = null;
 
     void Awake()
     {
         if (instance != null && instance != this)
         {
             Destroy(this.gameObject);
         }
         else
         {
             instance = this;
             DontDestroyOnLoad(gameObject);
         }
     }
     #endregion
 
     //this score persists throughout scenes because of the Singleton pattern
     private int score;
     /// <summary>
     /// Get and Set the score with this. When setting, the entered value is ADDED to the current score.
     /// </summary>
     public int Score
     {
         get{return score;}
         set{score += value;}
     }
     /// <summary>
     /// The first level of your game. If there is a menu, this may be 1, otherwise, it's probably 0.
     /// </summary>
     public int firstLevel;
     /// <summary>
     /// The currently loaded scene, set in the start function every level.
     /// </summary>
     private int currentScene;
     //the high score, pretty self explanatory
     private int highScore;
     /// <summary>
     /// The current high score of the player (Read-Only)
     /// </summary>
     public int HighScore
     {
         get { return highScore; }
     }
     /// <summary>
     /// A List of the scores required to advance to the next level. A main menu might change where the first value goes.
     /// </summary>
     public List<int> requiredScore = new List<int>();
 
     // Use this for initialization
     void Start()
     {
         //we get and store the current scene as an int here
         currentScene = GetSceneIndex(SceneManager.GetActiveScene());
 
         //if this is the first level...
         if (currentScene == firstLevel)
         {
             //set the score to 0
             score = 0;
             //get the high score from the save
             highScore = PlayerPrefs.GetInt("HighScore", 0);
         }
     }
     /// <summary>
     /// Returns the current scene index as an integer.
     /// </summary>
     /// <param name="scene"></param>
     /// <returns></returns>
     int GetSceneIndex(Scene scene)
     {
         int sceneIndex = scene.buildIndex;
         return sceneIndex;
     }
 
     // Update is called once per frame 
     void Update()
     {
         //if you want to save your high score every frame, uncomment the next line, although I think it's unnecessary
         SetHighscore(score, false);
 
         CheckForLevelChange();
     }
     /// <summary>
     /// Are the level score requirements fulfilled?
     /// </summary>
     void CheckForLevelChange()
     {
         //is the score equal to the required score for this level?
         if (score >= requiredScore[currentScene])
         {
             //load the scene additively (next scene in build)
             ChangeLevel(currentScene + 1);
         }
     }
     /// <summary>
     /// Load the specified scene and save the high score beforehand.
     /// </summary>
     /// <param name="sceneToBeLoaded"></param>
     public void ChangeLevel(int sceneToBeLoaded)
     {
         //save the high score first (for safety)
         SetHighscore(score, true);
         //load the scene
         SceneManager.LoadScene(sceneToBeLoaded);
     }
     /// <summary>
     /// Set the high score. If save is enabled, saves it to the PlayerPrefs.
     /// </summary>
     /// <param name="currentScore"></param>
     /// <param name="save"></param>
     public void SetHighscore(int currentScore, bool save)
     {
         //if the current score is higher than the previous high score...
         if (currentScore > highScore)
         {
             //set the high score
             highScore = currentScore;
 
             //if it should save the high score
             if (save)
             {
                 //save it
                 PlayerPrefs.SetInt("HighScore", highScore);
             }
         }
     }
 
     //are you sure you want to use the old IMGUI system? The new one is a lot better to use
     //I just left it in for now
     void OnGUI()
     {
         GUI.Label(new Rect(10, 10, 100, 20), "Score: " + score);
     }
     /// <summary>
     /// Has the player died? If so, save the score and load the first level
     /// </summary>
     public void OnDeath()
     {
         //save high score
         SetHighscore(score, true);
         //load the first level again
         ChangeLevel(firstLevel);
     }
 }



 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class GUIController : MonoBehaviour
 {
     //a Text showing the current score
     public Text ScoreText;
     //a Text showing the current high score
     public Text HighScoreText;
 
     void Update()
     {
         //set both text components
         ScoreText.text = LevelManager.instance.Score.ToString();
         HighScoreText.text = LevelManager.instance.HighScore.ToString();
     }
 }

I believe this should do it. IF you have more questions, just ask. Or send me an e-mail:

sol.software.soul@gmail.com

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BrodyDaChappy · Feb 06, 2016 at 04:37 PM 0
Share

Thanks Light,

it worked at first but once i hit 750 points which then loads next level the highscore is now stuck on 750 points and wont change at all.

Any Ideas?

avatar image Light997 BrodyDaChappy · Feb 06, 2016 at 06:15 PM 0
Share

So you're saying that the score still changes, but the highscore stays the same?

Btw, 2 quick tips:

  1. You can notify someone of your post by typing @(username), e.g., to inform me, you type @Light.

  2. If an answer solved your problem, accept it with the checkmark next to the answer. It should then turn green. This helps other people find the solution if they have the same problem.

avatar image Light997 Light997 · Feb 06, 2016 at 06:15 PM 0
Share

Also, now I forgot myself:

@BrodyDaChappy

Show more comments
avatar image Light997 · Feb 06, 2016 at 09:33 PM 0
Share

I think I've found it now: If you've made the changes I recommended, you are now reloading the high score from the Player prefs every frame.

I have a new version of the script to offer you, but I'll give it to you tomorrow since it's quite late now, I don't have access to a computer right now and editing code on a tablet is just impossible. I don't know where you live, so in time difference, tomorrow for me (about midday) is in 12- 13 hours (I live in Central Europe) I hope you can wait that long, otherwise, you'll have to find the errors on your own. I don't know what your game looks like, so I'll just tell you that you can work on controlling the character or the appearance of your GUI. Just some tips :) @BrodyDaChappy

avatar image BrodyDaChappy Light997 · Feb 06, 2016 at 09:45 PM 0
Share

That would be great @Light ! i do not mind waiting at all, in the meantime i will continue with other parts.

i live in the U$$anonymous$$ so it is pretty late here too, my game is 2D so not much to look at but some tips would be much appreciated.

Thanks again.

avatar image Light997 BrodyDaChappy · Feb 07, 2016 at 12:30 PM 1
Share

So here it is. I think this should do it. I heavily commented the scripts, and I also recommend using the GUIController script ins$$anonymous$$d of the OnGUI method, because it's much easier to work with.

Look at my answer (I edited it because I would otherwise exceed the character limit for comments, I didn't think I would ever reach it :)).

@BrodyDaChappy

avatar image Light997 · Feb 07, 2016 at 12:58 PM 1
Share

@BrodyDaChappy I looked at your history as well, and it seems you've been having a lot of trouble with your scores for a while now :)

So I hope this finally solves it ;)

avatar image BrodyDaChappy Light997 · Feb 07, 2016 at 09:02 PM 0
Share

@Light

Hi Light, thanks for all the above, i have now got unity 4.6.9 the GUIController Script shows error,

Assets/Scripts/OnGUI2D.cs(16,51): error CS0117: Level$$anonymous$$anager' does not contain a definition for instance'

What does this mean?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

41 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I access playerprefs from another script? 1 Answer

Hey i am trying to add a time score system and time high score system need help. 0 Answers

how do i store highscore locally C# (SIMPLE) 3 Answers

how to make highscore & score in quiz game with different scene,how to make highscore in different scene 1 Answer

Game over high score script 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges