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
3
Question by Strangerweather · Apr 28, 2016 at 01:22 PM · listssavingdatascore systemscoreboard

Saving Just Five Top Scores?

Hi guys, I'm a bit stuck with my lists and my savings. I want to save the date and time of the first 5 times a player reaches 10/10. After that, they can unlock a new stage in the game. What I have at the moment is this:

 void DisplayResults()
     {
         resultsText.text = "You scored " + numberOfCorrectAnswers + " out 10!";
 
         if (numberOfCorrectAnswers == 10)
         {
             PlayerData playerData = new PlayerData();
             playerData.topScoreDates.Add(DateTime.Now.ToString());
             ES2.Save(playerData.topScoreDates, "myFile.txt?tag=myList");
         }
 
     }

and this:

 public class PlayerData
 {
     public List<string> topScoreDates;
 
     public PlayerData()
     {
         topScoreDates = new List<string>();
     }
 }

And to retrieve the scores I have this:

         PlayerData playerData = new PlayerData();
         if (ES2.Exists("myFile.txt?tag=myList"))
             playerData.topScoreDates = ES2.LoadList<string>("myFile.txt?tag=myList");
         foreach (string score in playerData.topScoreDates)
         {
             print(score);
             scoreButton[0].GetComponentInChildren<Text>().text = score;
             scoreButton[1].GetComponentInChildren<Text>().text = score;
             scoreButton[2].GetComponentInChildren<Text>().text = score;
             scoreButton[3].GetComponentInChildren<Text>().text = score;
             scoreButton[4].GetComponentInChildren<Text>().text = score;
         }

The big problem is that what I am getting on my scoreboard is five times the same date and time. Also, this gets overwritten with each new win.

There is something about the list mechanism that I am obviously not getting. I would be grateful for some pointers. Many 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

1 Reply

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

Answer by HarshadK · Apr 28, 2016 at 01:57 PM

In your DisplayResults() method, you are creating PlayerData object each time you get a score of 10 so you get a new list each time you instantiate your PlayerData object to which you add your date and time. And when you save your list to file, you are overwriting the current list with this new list containing one value itself.

So before you add your date and time to the file and write your list to file, you actually need to have previous values that you added to your file to be present in the list. So you need to fetch and store the previous data to the list before adding the new item to the list. Then you can add your date and time to the list and then save the list.

You can reduce the reading of file each time date needs to be saved by using a Singleton pattern for PlayerData class.

Comment
Add comment · Show 6 · 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 · Apr 28, 2016 at 03:32 PM 0
Share

Thanks for your quick reply. I'm struggling to figure out how to do this in practice, but I'll accept your answer as I'm sure it's correct. :-)

Do I need to make an array of scores for this to work?

avatar image HarshadK Strangerweather · Apr 29, 2016 at 06:56 AM 2
Share

You need to make your PlayerData class to be:

 public class PlayerData
 {
     private static PlayerData mPlayerData;
     public static List<string> topScoreDates;

     public PlayerData Instance
     {
         get
         {
             if(mPlayerData == null) {
                 mPlayerData = this;
                 
                 if (ES2.Exists("myFile.txt?tag=myList"))
                     mPlayerData.topScoreDates = ES2.LoadList<string>("myFile.txt?tag=myList");
             }
             return mPlayerData;
         }
     }
 }

And ins$$anonymous$$d of creating an object of PlayerData using:

 PlayerData playerData = new PlayerData();

Just use:

 PlayerData playerData = PlayerData.Instance;
avatar image Strangerweather HarshadK · Apr 29, 2016 at 10:12 AM 1
Share

Thanks for this Harshad$$anonymous$$!

Show more comments

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

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

JsonUtility.ToJson returns empty file 2 Answers

Saving game respectively my level, when I restart my game. 0 Answers

gamemanager: monobehaviour, scriptable objects, or static class 1 Answer

C# find specific piece of data and where it is in list 1 Answer

Highscore with points and time 1 Answer


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