How to load a score form a saved game vs. the level just completed
In my game the player kills enemies and gets points for the enemies killed. If the player finishes the level it opens up a menu page where player has the option to save the game and/ or continue to the next level or start over. I am using playerprefs to carry the score between levels/scenes. However, when the player saves the game it goes into a text file.
My problem comes from when the player loads a saved game I want the score that was saved to load, however it takes the playerprefs score regardless. This results in the issue of say the player got a score of 67 the first time they played, decided to save the game and try again for a higher score. The second time around the player on scores 39, so doesn't save the game and just decides to load his saved game and continue from there. However, when he loads it, to his shock and horror, the score from his "saved game" is 39 and not 67.
I think I know this happens because the variable that is loaded from playerprefs, and that is the score that is displayed. I could make the variable that displays the score take the value from the text file, but then I have the opposite problem. I think my best option is to make a conditional statement that loads the score based on what the last level was, to determine if this is a saved game that is loading, or some other way to determine if the saved game loaded vs the current game. This is how I do it with the first level to reset the score to 0 if the player dies,
void Start () {
currentScene = SceneManager.GetActiveScene ();
if (currentScene.name == ("Level1")) { //if player is starting at Level1(a new game) the score is reset to 0
score = 0;
}
else {
score = PlayerPrefs.GetInt ("Player Score"); //if player is starting at any other level it gets and displays the accumulated player score from PlayerPrefs
UpdateScore ();
}
When the player completes the level the score is then loaded into playerprefs:
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Player")
{
PlayerPrefs.SetInt ("Player Score", score); //add the new score as a permanent score, if the player makes it to the next level
}
}
This is the code for displaying the score upon completion of the first level:
void Start () {
finalScore = PlayerPrefs.GetInt("Player Score");
Debug.Log (finalScore);
print ("Your final score is:" + finalScore); //displaying score recieved in that level
finalScoreText.text = "Your Final Score Is: " + finalScore;
}
If I changed the variable finalScore to the text file, like this : finalScore = ES2.Load("savedScore.txt");
it would load the text file instead.( I am using EasySave from the asset store, that is what the ES2 is).
What I think I would like to do is make a conditional statement stating that if the player loaded the game, or the last scene was the main menu from which the game would be loaded, finalScore = the saved text file. If the player is coming from the play scene, or any other scene really, so could be an else statement, then the finalScore = PlayerPrefs score.
However, in the scene manager library I could not find anything that would do this, there is no SceneManager.GetLastScene unfortunately. That is my problem, I don't know what to base my conditional statement on. I am certain the this is a common thing to do but cannot find if myself. If anyone has an idea or a better way to accomplish what I am trying to do any help would be greatly appreciated, thanks.
Your answer
Follow this Question
Related Questions
Passing Score from Game Scene to Game Over Scene usiing dontDestroyOnLoad C# 1 Answer
Spawning into a new scene with Game Manager 0 Answers
Save and load scene from file 2 Answers
PlayerPref doesnt work 2 Answers
Highscore system unity C# 0 Answers