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
}
}
}
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);
}
}
}
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.
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:
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?
So you're saying that the score still changes, but the highscore stays the same?
Btw, 2 quick tips:
You can notify someone of your post by typing @(username), e.g., to inform me, you type @Light.
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.
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
That would be great @Light ! i do not $$anonymous$$d 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.
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 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 ;)
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?