Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by MarcopoloR · Mar 30, 2016 at 11:40 AM · scene-loadingloadinglevelsave datascore system

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.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges