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 khitjuan2629 · Oct 10, 2018 at 08:38 AM · save datafile-ioserializableloading filebinaryformatter

My app generates a binary file but either fails to save data or load it || Unity for Android

Here's the problem: My script that produces a binary file works on pc, but fails on Android.


 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class GameControl : MonoBehaviour {
 
   public static GameControl gameControl;
 
   [Header ("Store Variables")]
   public float coins;
 
   [Header("Time Variables")]
   public DateTime currentDate;
   public DateTime yesterdayDate;
 
   [Header("Record Variables")]
   public int bestScore;
 
   [Header ("Inventory Variables")]
   public int numberOfHeartBoosts;
   public int numberOfTimeBoosts;
   public int numberOfLockBoosts;
 
   [Header("Settings Variables")]
   public bool musicOn;
   public float musicPercentage;
   public bool sfxOn;
   public float sfxPercentage;
   public bool isTutorialActive;
 
   [Header("Design Variables")]
   public float buttonFontColorR = 1f;
   public float buttonFontColorG = 0.5882353f;
   public float buttonFontColorB = 0f;
   public float buttonInnerBoxColorR = 0f;
   public float buttonOuterBoxColorR = 1f;
   public float buttonInnerBoxColorG = 0f;
   public float buttonOuterBoxColorG = 0.5882353f;
   public float buttonInnerBoxColorB = 0f;
   public float buttonOuterBoxColorB = 0f;
   public float themeColorR = 1f;
   public float themeColorG = 1f;
   public float themeColorB = 1f;
   public float backgroundColorR = 1f;
   public float backgroundColorG = 0.5882353f;
   public float backgroundColorB = 0f;
 
   [Header("Store Button Unlockables")]
   public bool isButtonOnePurchased;
   public bool isButtonTwoPurchased;
   public bool isButtonThreePurchased;
   public bool isButtonFourPurchased;
   public bool isButtonFivePurchased;
   public bool isButtonSixPurchased;
   public bool isButtonSevenPurchased;
   public bool isButtonEightPurchased;
   public bool isButtonNinePurchased;
 
   [Header("Store Theme Unlockables")]
   public bool isThemeOnePurchased;
   public bool isThemeTwoPurchased;
   public bool isThemeThreePurchased;
   public bool isThemeFourPurchased;
   public bool isThemeFivePurchased;
   public bool isThemeSixPurchased;
   public bool isThemeSevenPurchased;
   public bool isThemeEightPurchased;
   public bool isThemeNinePurchased;
 
   [Header("Store Background Unlockables")]
   public bool isBackgroundOnePurchased;
   public bool isBackgroundTwoPurchased;
   public bool isBackgroundThreePurchased;
   public bool isBackgroundFourPurchased;
   public bool isBackgroundFivePurchased;
   public bool isBackgroundSixPurchased;
   public bool isBackgroundSevenPurchased;
   public bool isBackgroundEightPurchased;
   public bool isBackgroundNinePurchased;
 
   void OnDisable(){
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath + "/PlayerData.eut");
 
     PlayerData data = new PlayerData();
 
     data.currentDate = currentDate;
     data.yesterdayDate = yesterdayDate;
     data.coins = coins;
     data.bestScore = bestScore;
     data.numberOfHeartBoosts = numberOfHeartBoosts;
     data.numberOfTimeBoosts = numberOfTimeBoosts;
     data.numberOfLockBoosts = numberOfLockBoosts;
     data.musicOn = musicOn;
     data.musicPercentage = musicPercentage;
     data.sfxOn = sfxOn;
     data.sfxPercentage = sfxPercentage;
     data.isTutorialActive = isTutorialActive;
     data.isButtonOnePurchased = isButtonOnePurchased;
     data.isButtonTwoPurchased = isButtonTwoPurchased;
     data.isButtonThreePurchased = isButtonThreePurchased;
     data.isButtonFourPurchased = isButtonFourPurchased;
     data.isButtonFivePurchased = isButtonFivePurchased;
     data.isButtonSixPurchased = isButtonSixPurchased;
     data.isButtonSevenPurchased = isButtonSevenPurchased;
     data.isButtonEightPurchased = isButtonEightPurchased;
     data.isButtonNinePurchased = isButtonNinePurchased;
 
     data.buttonFontColorR = buttonFontColorR;
     data.buttonFontColorG = buttonFontColorG;
     data.buttonFontColorB = buttonFontColorB;
 
     data.buttonInnerBoxColorR = buttonInnerBoxColorR;
     data.buttonInnerBoxColorG = buttonInnerBoxColorG;
     data.buttonInnerBoxColorB = buttonInnerBoxColorB;
 
     data.buttonOuterBoxColorR = buttonOuterBoxColorR;
     data.buttonOuterBoxColorG = buttonOuterBoxColorG;
     data.buttonOuterBoxColorB = buttonOuterBoxColorB;
 
     data.isThemeOnePurchased = isThemeOnePurchased;
     data.isThemeTwoPurchased = isThemeTwoPurchased;
     data.isThemeThreePurchased = isThemeThreePurchased;
     data.isThemeFourPurchased = isThemeFourPurchased;
     data.isThemeFivePurchased = isThemeFivePurchased;
     data.isThemeSixPurchased = isThemeSixPurchased;
     data.isThemeSevenPurchased = isThemeSevenPurchased;
     data.isThemeEightPurchased = isThemeEightPurchased;
     data.isThemeNinePurchased = isThemeNinePurchased;
 
     data.themeColorR = themeColorR;
     data.themeColorG = themeColorG;
     data.themeColorB = themeColorB;
 
     data.isBackgroundOnePurchased = isBackgroundOnePurchased;
     data.isBackgroundTwoPurchased = isBackgroundTwoPurchased;
     data.isBackgroundThreePurchased = isBackgroundThreePurchased;
     data.isBackgroundFourPurchased = isBackgroundFourPurchased;
     data.isBackgroundFivePurchased = isBackgroundFivePurchased;
     data.isBackgroundSixPurchased = isBackgroundSixPurchased;
     data.isBackgroundSevenPurchased = isBackgroundSevenPurchased;
     data.isBackgroundEightPurchased = isBackgroundEightPurchased;
     data.isBackgroundNinePurchased = isBackgroundNinePurchased;
 
     data.backgroundColorR = backgroundColorR;
     data.backgroundColorG = backgroundColorG;
     data.backgroundColorB = backgroundColorB;
 
     bf.Serialize(file, data);
     file.Close();
   }
 
   void OnEnable(){
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Open(Application.persistentDataPath + "/PlayerData.eut", FileMode.Open);
     PlayerData data = (PlayerData)bf.Deserialize(file);
     file.Close();
 
     currentDate = data.currentDate;
     yesterdayDate = data.yesterdayDate;
     coins = data.coins;
     bestScore = data.bestScore;
     numberOfHeartBoosts = data.numberOfHeartBoosts;
     numberOfTimeBoosts = data.numberOfTimeBoosts;
     numberOfLockBoosts = data.numberOfLockBoosts;
     musicOn = data.musicOn;
     musicPercentage = data.musicPercentage;
     sfxOn = data.sfxOn;
     sfxPercentage = data.sfxPercentage;
     isTutorialActive = data.isTutorialActive;
     isButtonOnePurchased = data.isButtonOnePurchased;
     isButtonTwoPurchased = data.isButtonTwoPurchased;
     isButtonThreePurchased = data.isButtonThreePurchased;
     isButtonFourPurchased = data.isButtonFourPurchased;
     isButtonFivePurchased = data.isButtonFivePurchased;
     isButtonSixPurchased = data.isButtonSixPurchased;
     isButtonSevenPurchased = data.isButtonSevenPurchased;
     isButtonEightPurchased = data.isButtonEightPurchased;
     isButtonNinePurchased = data.isButtonNinePurchased;
 
     buttonFontColorR = data.buttonFontColorR;
     buttonInnerBoxColorR = data.buttonInnerBoxColorR;
     buttonOuterBoxColorR = data.buttonOuterBoxColorR;
     buttonFontColorG = data.buttonFontColorG;
     buttonInnerBoxColorG = data.buttonInnerBoxColorG;
     buttonOuterBoxColorG = data.buttonOuterBoxColorG;
     buttonFontColorB = data.buttonFontColorB;
     buttonInnerBoxColorB = data.buttonInnerBoxColorB;
     buttonOuterBoxColorB = data.buttonOuterBoxColorB;
 
     isThemeOnePurchased = data.isThemeOnePurchased;
     isThemeTwoPurchased = data.isThemeTwoPurchased;
     isThemeThreePurchased = data.isThemeThreePurchased;
     isThemeFourPurchased = data.isThemeFourPurchased;
     isThemeFivePurchased = data.isThemeFivePurchased;
     isThemeSixPurchased = data.isThemeSixPurchased;
     isThemeSevenPurchased = data.isThemeSevenPurchased;
     isThemeEightPurchased = data.isThemeEightPurchased;
     isThemeNinePurchased = data.isThemeNinePurchased;
 
     themeColorR = data.themeColorR;
     themeColorG = data.themeColorG;
     themeColorB = data.themeColorB;
 
     isBackgroundOnePurchased = data.isBackgroundOnePurchased;
     isBackgroundTwoPurchased = data.isBackgroundTwoPurchased;
     isBackgroundThreePurchased = data.isBackgroundThreePurchased;
     isBackgroundFourPurchased = data.isBackgroundFourPurchased;
     isBackgroundFivePurchased = data.isBackgroundFivePurchased;
     isBackgroundSixPurchased = data.isBackgroundSixPurchased;
     isBackgroundSevenPurchased = data.isBackgroundSevenPurchased;
     isBackgroundEightPurchased = data.isBackgroundEightPurchased;
     isBackgroundNinePurchased = data.isBackgroundNinePurchased;
 
     backgroundColorR = data.backgroundColorR;
     backgroundColorG = data.backgroundColorG;
     backgroundColorB = data.backgroundColorB;
   }
 
     void Awake(){
     if (gameControl == null){
         DontDestroyOnLoad(gameObject);
       gameControl = this;
     }
     else if (gameControl != this){
       Destroy(gameObject);
     }
   }
 }
 
 [Serializable]
 class PlayerData{
   public DateTime currentDate;
   public DateTime yesterdayDate;
   public float coins;
   public int bestScore;
   public int numberOfHeartBoosts;
   public int numberOfTimeBoosts;
   public int numberOfLockBoosts;
   public bool musicOn;
   public float musicPercentage;
   public bool sfxOn;
   public float sfxPercentage;
   public bool isTutorialActive;
   public bool isButtonOnePurchased;
   public bool isButtonTwoPurchased;
   public bool isButtonThreePurchased;
   public bool isButtonFourPurchased;
   public bool isButtonFivePurchased;
   public bool isButtonSixPurchased;
   public bool isButtonSevenPurchased;
   public bool isButtonEightPurchased;
   public bool isButtonNinePurchased;
 
   public float buttonFontColorR;
   public float buttonFontColorG;
   public float buttonFontColorB;
   public float buttonInnerBoxColorR;
   public float buttonOuterBoxColorR;
   public float buttonInnerBoxColorG;
   public float buttonOuterBoxColorG;
   public float buttonInnerBoxColorB;
   public float buttonOuterBoxColorB;
 
   public bool isThemeOnePurchased;
   public bool isThemeTwoPurchased;
   public bool isThemeThreePurchased;
   public bool isThemeFourPurchased;
   public bool isThemeFivePurchased;
   public bool isThemeSixPurchased;
   public bool isThemeSevenPurchased;
   public bool isThemeEightPurchased;
   public bool isThemeNinePurchased;
 
   public float themeColorR;
   public float themeColorG;
   public float themeColorB;
 
   public bool isBackgroundOnePurchased;
   public bool isBackgroundTwoPurchased;
   public bool isBackgroundThreePurchased;
   public bool isBackgroundFourPurchased;
   public bool isBackgroundFivePurchased;
   public bool isBackgroundSixPurchased;
   public bool isBackgroundSevenPurchased;
   public bool isBackgroundEightPurchased;
   public bool isBackgroundNinePurchased;
 
   public float backgroundColorR;
   public float backgroundColorG;
   public float backgroundColorB;
 }


I've been stuck for days figuring out what is wrong with my script. I keep digging old threads and experimenting on my code, but nothing works. Forgive the way I code, I'm not really a programmer but the deadline is near, I'm working as fast as I could. Thanks in advance!

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
0
Best Answer

Answer by Bunny83 · Oct 09, 2018 at 10:19 PM

You shouldn't do this:

 Application.persistentDataPath + "/PlayerData.eut"

You can't be sure how the persistentDataPath ends. If it ends with a slash you would have a double slash. You should always use System.IO.Path.Combine to concat a path with a filename

 Path.Combine(Application.persistentDataPath, "PlayerData.eut")

I can't say for sure that this is the issue. There could be other things wrong. You have to debug this on your end. Check the logcat of your device.


Keep in mind that when an application is moved to the background, OnDisabled will not be called. You may want to use OnApplicationFocus or something similar. Once an app is in the background the app could be terminated at any time. Saving when loosing focus should only be a safety mechanism. It's better to save in between as long as the application still runs.

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 khitjuan2629 · Oct 10, 2018 at 12:40 AM 0
Share

thanks for the fix! Its either the path goes to the wrong direction or it fails to save because of the case when OnDisable doesn't get called. I definitely learned stuff today, thanks again!

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

90 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

Related Questions

JSON not saving game object list 0 Answers

Save and (later) read scriptable objects 1 Answer

Serialized data show information after load 0 Answers

how to have save and load system. With this programing,How to do a save and load system on mobile. With this programing 1 Answer

[Android] Save data loss with PlayerPrefs? 2 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