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 manuelmangual · Aug 29, 2016 at 07:00 PM · c#unity 5playerprefs

My save data is being overwritten

Hello, world:

I'm writing a very easy save function in Unity for an iOS game. Though it saves your progress as it goes, however, it deletes the data from all the previously saved levels. Can anyone please help me understand what I am doing wrong? Before I post my code let me provide you with a visual of what is happening right now.

alt text

The image above demonstrates what happens after I save my game data and for this part only, it does it right.

But...

When I go to save the data after completing the second level, this suddenly happens:

alt text

Level 2 data is saved but the first level data is gone. This is the case for all the other levels.

Here is the code I am using to save the data:

 public void Save(int _collectibleCount, int _starCount, int _minutes, int _seconds)
     {
         SerializableLevelData.LastKeyIndex = 0;
 
         var _level = new LevelContainer();
         
         for (int i = 0; i < PlayerPrefs.GetInt("LevelCounter"); i++)
         {
             _level.Container.Add(new SerializableLevelData());
 
             if (SceneManager.GetActiveScene().name == _level.Container[i].Key)
             {
                 _level.Container[i].CollectibleCount = (_collectibleCount > _level.Container[i].CollectibleCount) ? _collectibleCount : _level.Container[i].CollectibleCount;
                 _level.Container[i].GoldStarsCount = (_starCount > _level.Container[i].GoldStarsCount) ? _starCount : _level.Container[i].GoldStarsCount;
                 _level.Container[i].MinutesCounter = (_level.Container[i].MinutesCounter == 0 || _minutes < _level.Container[i].MinutesCounter) ? _minutes : _level.Container[i].MinutesCounter;
                 _level.Container[i].SecondsCounter = (_level.Container[i].SecondsCounter == 0 || _seconds < _level.Container[i].SecondsCounter) ? _seconds : _level.Container[i].SecondsCounter;
             }
         }
         
         PlayerPrefs.SetInt(Constants.LevelFinishedCounter, _levelData.Container.Count -1);
 
         // This is the final resulting JSON from the serialization process
         CreateFile(_level);
     }
 
     // Finally our save and load methods for the file itself
     void CreateFile(LevelContainer _container)
     {
         PlayerPrefs.SetString("SaveData", JsonUtility.ToJson(_container, true));
 
         Debug.Log("File written.");
     }

Some observation though: I am using PlayerPrefs to work around a UnauthorizedAccessException issue I was having before.

level1.png (15.8 kB)
level2.png (16.9 kB)
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

I'm having a problem while jumping an object in Unity 5 0 Answers

How to get the active/loaded Scene then turn the name of it into a string? [C#] 1 Answer

Score value in GameOver screen is 0 instead of final value from the Game Level scene(used PlayerPrefs), how do I display the final value from Game Level scene in the GameOver scene? 1 Answer

PlayerPrefs is affecting all builds of my game 1 Answer

Set the username with an inputfield ? 3 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