Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 fireburner80 · Feb 12 at 06:03 AM · save datasave file

Saved data reverts when ending editor session

I have a script written originally in Unity 2018 and now in Unity 2020 I have a weird bug. I save player data to a file called playerdata.dat. When the game starts, the file is created if it's not there and is updated when I call the savePlayerData function. I can then update values and saving the file shows an updated timestamp on the file in the folder.

Here's the problem: Ending the game session in the editor changes the values in the files. Even the Console values change. For example: When hitting the play button, data is loaded in from the file. MusicVolume is 0.05. I change the value to 0 and save the data. I use Debug.Log to display the value and it shows as 0. I hit the play button and the value displayed in the Console that was 0 changes to 0.05 (in the Console. The displayed value actually changes). Hitting the play button again starts the musicVolume of playerData at 0.05 (the default). Changing the value in the editor does NOT change the displayed console value.

I've confirmed the "if (playerData == null)" statement never ends up being true.

 public void savePlayerData()
 {
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath + "/" +"playerData.dat");
     if (playerData == null)
     {
         playerData = new PlayerData();
         playerData.wingLength = 1;
         playerData.wingWidth = 1;
         playerData.tailTrail = 1;
         playerData.tailWidth = 1;
         playerData.tailLength = 1;
         playerData.bodySize = 1;
         playerData.raceTimeRecord = 999999999;
         playerData.racePointsPerTimeRecord = 0;
         playerData.gameVolume = 1f;
         playerData.musicVolume = 0.05f;
         playerData.resolution = 123456789;
         playerData.location = 0;
         playerData.dragonColor = "yellow";
         playerData.playerID = "0123456789";
         playerData.raceIndicator = true;
         playerData.musicEnabled = true;
         playerData.muted = false;
         playerData.demonKills = 0;
         playerData.spiderKills = 0;
         playerData.crabKills = 0;
         playerData.borkGateHit = false;
         playerData.tenMinuteFlight = false;
         playerData.twentyMinuteFlight = false;
         Debug.Log("new player data created");
     }
     bf.Serialize(file, playerData);
     file.Close();
     Debug.Log("Player data saved successfully");
     Debug.Log(playerData.musicVolume.ToString());
 }

 public void loadPlayerData()
 {
     if (File.Exists(Application.persistentDataPath + "/" + "playerData.dat"))
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.OpenRead(Application.persistentDataPath + "/playerData.dat");
         playerData = (PlayerData)bf.Deserialize(file);
         file.Close();
         Debug.Log("Player data loaded successfully");
     }
     else
     {
         this.savePlayerData();
     }
     Debug.Log(playerData.musicVolume.ToString());
 }

So, why would ending a test session reset values in a saved file?

While the game was running, I copied the current version of the file and changed the name. I ended the session, saw the bug, deleted playerData.dat, and renamed the copied file to playerData.dat. When the game loaded in, the value was correct (0 which is what was saved). When I end the game session and start it again, though, it goes back to the default of 0.05.

Comment
Add comment · Show 2
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 Captain_Pineapple · Feb 12 at 09:11 AM 0
Share

is playerData a static variable?

avatar image fireburner80 Captain_Pineapple · Feb 14 at 10:02 AM 0
Share

Good question. It's a Serializable public class. I confirmed that an actual build of the game saves and loads data as expected. Even if I set up the file correctly, change the value in the editor, save, close Unity, and reopen the project, the value will load in correctly upon running the game but revert when ending the session. It seems to be something in the editor that is reverting the value.

[Serializable] public class PlayerData { public float highScore; public float highScoreTime; public float skyHighScore; public float skyHighScoreTime; public float adjustedHighScore; public float wingLength; public float wingWidth; public float tailTrail; public float tailWidth; public float tailLength; public float bodySize; public float raceTimeRecord; public float racePointsPerTimeRecord; public float gameVolume; public float musicVolume; public int resolution; public int location; public string dragonColor; public string playerID; public bool raceIndicator; public bool musicEnabled; public bool muted;

 public int demonKills;
 public int spiderKills;
 public int crabKills;
 public bool borkGateHit;
 public bool tenMinuteFlight;
 public bool twentyMinuteFlight;

}

1 Reply

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

Answer by fireburner80 · Feb 18 at 03:46 AM

I figured it out!!!

When you end the game session in the editor by hitting the play button, the game resets all sliders and toggles to back to what they were before the game session started.

HOWEVER, it does this while the game is running. If you have the toggles or sliders set to call a function when the value is changed, those functions will be called BEFORE the game shuts down.

I have the toggles and sliders set to update a value in the playerdata object and immediately save the data which is why all the values were being overwritten. They were being overwritten by the original values of the sliders from before the session started and not the values set in the playerdata object.

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

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

134 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs not saving in build 1 Answer

Save scene 1 Answer

if I save my progress data in a file at a persistence data path on IOS and android, will it delete my old data if I update the game on IOS and android...help me out... 0 Answers

Will appending a save file keep your save after an update? 0 Answers

Save script not saving data on android device 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