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 /
avatar image
0
Question by Strangerweather · Jan 12, 2016 at 08:00 AM · serializationsavesceneshighscores

How to set and save individual highscores for each scene using serialization?

I have followed the Unity live tutorial on Data Persistence as I understand it is much better than PlayerPrefs for saving scores etc. However, I find that the high score that I saved in Scene 1 persists into my other scenes. How would I go about setting and saving separate high scores with my serialization script?

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

3 Replies

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

Answer by Munchy2007 · Jan 12, 2016 at 10:11 AM

You just need to give each scene its own high score entry in your saved data and then load the corresponding entry when each scene loads.

PseudoCode...

 void SaveScore()
 {
       SceneNameHiScore = sceneHiScore;
       SceneNameHiScore.Save();
 }
 
 void OnSceneWasLoaded()
 {
       sceneHiScore = SceneNameHiScore.Load();
 }

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

Answer by Munchy2007 · Jan 12, 2016 at 08:26 PM

Here's a quick implementation of a class for saving and loading an array of high scores. You can access the individual scores by level number.

 using UnityEngine;
 using System.Collections;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 [System.Serializable]
 public class PlayerData  {
     int[] hiScores;
 
     public int maxLevels {
         get;
         set;
     }
 
     protected PlayerData()
     {
     }
 
     public PlayerData (int maxLevels)
     {
         this.maxLevels = maxLevels;
         hiScores = new int[maxLevels];
     }
 
     public void SetScore(int levelNumber, int score)
     {
         hiScores[levelNumber] = score;
     }
 
     public int GetScore(int levelNumber)
     {
         return hiScores[levelNumber];
     }
 
     public void Save(string path)
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(path);   
 
         bf.Serialize(file, this);
         file.Close();
     }
 
     public static PlayerData Load(string path)
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(path, FileMode.Open);   
 
         PlayerData data = (PlayerData)bf.Deserialize(file);
         file.Close();
         return data;
     }
 }
 

For simplicity the array size is set when you create an instance of the class, but this could be improved on to make it dynamic.

Put this test script on an empty game object and it should debug.log 50 scores.

 using UnityEngine;
 using System.Collections;
 
 public class TestPlayerData : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         PlayerData playerData = new PlayerData(50);
 
         // Insert some test data
         for(int levelNum=0; levelNum< playerData.maxLevels; levelNum++)
         {
             playerData.SetScore(levelNum, (levelNum+1) * Random.Range(500,1000));
         }
 
         Debug.Log("Saving data...");
         playerData.Save(Application.persistentDataPath + "/PlayerInfo.dat");
 
         Debug.Log("Loading data...");
         playerData = PlayerData.Load(Application.persistentDataPath + "/PlayerInfo.dat");
 
         for(int levelNum = 0; levelNum<50;levelNum++)
         {
             Debug.Log(playerData.GetScore(levelNum));
         }
     }
 
     // Update is called once per frame
     void Update () {
         
     }
 }
 

Hopefully this is of some help to you :)

Comment
Add comment · Show 1 · 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 Strangerweather · Jan 12, 2016 at 09:52 PM 1
Share

That's fabulous, thanks a lot! Hopefully it'll be helpful to other people too!

avatar image
0

Answer by Strangerweather · Jan 12, 2016 at 01:29 PM

Many thanks for your reply! I am still a bit confused. This is what I did:

  public void Save ()
     {
 
 
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");    
        
         PlayerData data = new PlayerData();
         data.highScore1 = highScore1;
         data.highScore2 = highScore2;
         data.highScore3 = highScore3;
 
         bf.Serialize(file, data);
         file.Close();
         
     }
     
     public void Load()
     {
          if (File.Exists(Application.persistentDataPath+ "/playerInfo.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize(file);
             file.Close();
 
             highScore1 = data.highScore1;
             highScore2 = data.highScore2;
             highScore3 = data.highScore3;
         }
     }

The problem is my ScoreManager:

  void Start()
     {
         GameControl.control.Load();
         highScoreText.text = "High Score: " + Mathf.Round(GameControl.control.highScore);
     }
 
 
     void Update()
     {
         if (scoreIncreasing)
         {
             scoreCount += minusPointsPerSecond * Time.deltaTime;
         }
         scoreText.text = "Score: " + Mathf.Round(scoreCount);
     }
 
     public void SetHighScore()
     {
         if (scoreCount > GameControl.control.highScore)
         {
             GameControl.control.highScore = scoreCount;
         }
         highScoreText.text = "High Score: " + Mathf.Round(GameControl.control.highScore);
         GameControl.control.Save();
     }

Do I also need 3 separate scoreCounts and 3 separate scoreTexts? And finally, how can I associate the scenes with the highscores? Is it done manually in the Inspector?

Comment
Add comment · Show 3 · 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 Munchy2007 · Jan 12, 2016 at 02:30 PM 0
Share

You should only need the one scoreText and scoreCount as these are look like they are just used to handle the score within the current level.

What you do need is some way to associate each scene with the appropriate entry in your PlayerData class.

You haven't posted your PlayerData class implementation, but from your code it doesn't look like it easily lends itself to doing what you want.

I think if I was doing it, I would use a string,int dictionary to store the high scores, with the key being the scene name. If you need more help with this let me know and I'll see if I can throw a quick example together.

avatar image Strangerweather Munchy2007 · Jan 12, 2016 at 03:51 PM 0
Share

Thanks so much that's very kind of you. It would be great if you could throw a quick example together as you suggest because I have been trying to do exactly that for days and I am not getting anywhere. Also, I have read that dictionaries can't be serialized. Will that not be a problem? Thanks again!

avatar image Munchy2007 Strangerweather · Jan 12, 2016 at 04:41 PM 1
Share

That's very true, I forgot about that, although there are some solutions to dictionary serialisation kicking around (http://answers.unity3d.com/questions/460727/how-to-serialize-dictionary-with-unity-serializati.html). However, in this instance it would probably be easier to implement the PlayerData class differently, so that it can be serialised.

I'm leaving work in a few $$anonymous$$utes, so I'll have to wait until I get home before I can take another look at it. Unless anyone else provides a solution in the meantime I'll see if I can work out a reasonably elegant solution a bit later tonight.

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

34 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

Related Questions

Displaying highscore in the main menu scene 1 Answer

Serialized data not getting saved in iPhone 1 Answer

Local High-Scores with PlayerPrefs? 1 Answer

Unity Serializer scripting error 1 Answer

Saving using Serialization and IDs 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