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 mitchellson97 · Jan 13, 2016 at 05:45 PM · serializationloadingsaving

Loading and Saving error

Hi,

I recently followed the online tutorial step by step on how to save and load data using serialization, once I completed it and ran it (I have a button to save and a button to load) when the save button is pressed I can easily get to the file and see that in fact all the data has saved but when I try to load it again nothing happens all the data is once again reset to default values, the full code is below and I would really appreciate it if you could help in any way.

Code:

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using UnityEngine.UI;
 
 
 
 [Serializable]
 public class Services
 {
     public string name;
     public int noUnits;
     public int avalUnits;
     public string fundingLevel;
     public int level;
 
     public Services()
     {
         name = "Police";
         noUnits = 1;
         avalUnits = 1;
         fundingLevel = "unassigned";
         level = 1;
     }
 
 }
 [Serializable]
 public class Missions
 {
     public int difficulty;
     public bool police;
     public bool fire;
     public bool rescue;
     public int p_units;
     public int f_units;
     public int r_units;
     public double baseChance;
     public double totalChance;
     public int payOutAmount;
     public int unitsLost;
     public bool missionUndertaken;
     public bool MissionWon;
     public double result;
 
     public Missions()
     {
         difficulty = 1;
         police = true;
         fire = true;
         rescue = true;
         p_units = 0;
         f_units = 0;
         r_units = 0;
         baseChance = 0.5000;
         totalChance = 0.5000;
         payOutAmount = 50;
         unitsLost = 0;
         missionUndertaken = true;
         MissionWon = true;
     }
 };
 
 [Serializable]
 public class Game
 {
     public int money;
     public string time;
     public string date;
     public int missionsComplete;
     public bool gamePaused;
     public bool gameLost;
     public bool tutorials;
 
     public Services p = new Services();
     public Services f = new Services();
     public Services r = new Services();
 
     public Missions m1 = new Missions();
     public Missions m2 = new Missions();
     public Missions m3 = new Missions();
     public Missions m4 = new Missions();
     public Missions m5 = new Missions();
 
     public Game()
     {
         money = 50;
         time = "12:00";
         date = "1st January 2016";
         missionsComplete = 0;
         gamePaused = false;
         gameLost = false;
         tutorials = true;
 
         p.name = "Police";
         p.noUnits = 1;
         p.avalUnits = 1;
         p.fundingLevel = "Good";
         p.level = 1;
 
         f.name = "Fire";
         f.noUnits = 1;
         f.avalUnits = 1;
         f.fundingLevel = "Good";
         f.level = 1;
 
         r.name = "Fire";
         r.noUnits = 1;
         r.avalUnits = 1;
         r.fundingLevel = "Good";
         r.level = 1;
 
         m1.difficulty = 1;
         m1.police = true;
         m1.fire = false;
         m1.rescue = false;
         m1.p_units = 0;
         m1.f_units = 0;
         m1.r_units = 0;
         m1.baseChance = 1.0;
         m1.totalChance = 1.0;
         m1.payOutAmount = 50;
         m1.unitsLost = 0;
         m1.missionUndertaken = false;
         m1.MissionWon = false;
 
         m2.difficulty = 2;
         m2.police = true;
         m2.fire = true;
         m2.rescue = false;
         m2.p_units = 0;
         m2.f_units = 0;
         m2.r_units = 0;
         m2.baseChance = 0.55;
         m2.totalChance = 0.6;
         m2.payOutAmount = 75;
         m2.unitsLost = 0;
         m2.missionUndertaken = false;
         m2.MissionWon = false;
 
         m3.difficulty = 3;
         m3.police = true;
         m3.fire = true;
         m3.rescue = false;
         m3.p_units = 0;
         m3.f_units = 0;
         m3.r_units = 0;
         m3.baseChance = 0.5;
         m3.totalChance = 0.6;
         m3.payOutAmount = 100;
         m3.unitsLost = 0;
         m3.missionUndertaken = false;
         m3.MissionWon = false;
 
         m4.difficulty = 4;
         m4.police = true;
         m4.fire = true;
         m4.rescue = false;
         m4.p_units = 0;
         m4.f_units = 0;
         m4.r_units = 0;
         m4.baseChance = 0.45;
         m4.totalChance = 0.6;
         m4.payOutAmount = 125;
         m4.unitsLost = 0;
         m4.missionUndertaken = false;
         m4.MissionWon = false;
 
         m5.difficulty = 5;
         m5.police = true;
         m5.fire = true;
         m5.rescue = false;
         m5.p_units = 0;
         m5.f_units = 0;
         m5.r_units = 0;
         m5.baseChance = 0.4;
         m5.totalChance = 0.6;
         m5.payOutAmount = 150;
         m5.unitsLost = 0;
         m5.missionUndertaken = false;
         m5.MissionWon = false;
     }
 };
 [Serializable]
 public class Chance : MonoBehaviour
 {
 
     public Text moneyText;
     public Text timeText;
     public Text dateText;
 
     public Text pUnitsText;
     public Text fUnitsText;
     public Text rUnitsText;
 
     public Text pAvalUnitsText;
     public Text fAvalUnitsText;
     public Text rAvalUnitsText;
 
     public Text pFundingText;
     public Text fFundingText;
     public Text rFundingText;
 
     public Canvas tutorialCanvas;
     public Canvas pUpgradeCanvas;
     public Canvas fUpgradeCanvas;
     public Canvas rUpgradeCanvas;
     public Canvas noMoneyCanvas;
     public Canvas noUnitsCanvas;
 
     public Game game;
 
 
     public void Awake()
     {
         game = new Game();
     }
 
     // Use this for initialization
     public void Start()
     {
         moneyText.text = "£" + game.money.ToString();
         timeText.text = game.time.ToString();
         dateText.text = game.date.ToString();
         pUnitsText.text = "No. of units: " + game.p.noUnits.ToString();
         fUnitsText.text = "No. of units: " + game.f.noUnits.ToString();
         rUnitsText.text = "No. of units: " + game.r.noUnits.ToString();
 
         pAvalUnitsText.text = "Available units:  " + game.p.avalUnits.ToString();
         fAvalUnitsText.text = "Available units:  " + game.f.avalUnits.ToString();
         rAvalUnitsText.text = "Available units:  " + game.r.avalUnits.ToString();
 
         pFundingText.text = "Funding Level:  " + game.p.fundingLevel.ToString();
         fFundingText.text = "Funding Level:  " + game.p.fundingLevel.ToString();
         rFundingText.text = "Funding Level:  " + game.p.fundingLevel.ToString();
 
     }
 
     public void pPlus()
     {
         if (game.money >= 50)
         {
             game.p.noUnits = game.p.noUnits + 1;
             pUnitsText.text = "No. Of Units " + game.p.noUnits.ToString();
             game.p.avalUnits = game.p.avalUnits + 1;
             pAvalUnitsText.text = "Available units  " + game.p.avalUnits.ToString();
             game.money = game.money - 50;
             moneyText.text = "£" + game.money.ToString();
         }
 
         else
         {
             noMoneyCanvas.enabled = true;
         }
     }
 
     public void pMinus()
     {
         if (game.p.noUnits >= 1)
         {
             game.p.noUnits = game.p.noUnits - 1;
             pUnitsText.text = "No. Of Units " + game.p.noUnits.ToString();
             game.p.avalUnits = game.p.avalUnits - 1;
             pAvalUnitsText.text = "Available units  " + game.p.avalUnits.ToString();
             game.money = game.money + 50;
             moneyText.text = "£" + game.money.ToString();
         }
 
         if (game.p.noUnits < 0)
         {
             game.p.noUnits = 0;
             game.p.avalUnits = 0;
             noUnitsCanvas.enabled = true;
         }
     }
 
     public void fPlus()
     {
         if (game.money >= 50)
         {
             game.f.noUnits = game.f.noUnits + 1;
             fUnitsText.text = "No. Of Units " + game.f.noUnits.ToString();
             game.f.avalUnits = game.f.avalUnits + 1;
             fAvalUnitsText.text = "Available units  " + game.f.avalUnits.ToString();
             game.money = game.money - 50;
             moneyText.text = "£" + game.money.ToString();
         }
 
         else
         {
             noMoneyCanvas.enabled = true;
         }
     }
 
     public void fMinus()
     {
         if (game.f.noUnits >= 1)
         {
             game.f.noUnits = game.f.noUnits - 1;
             fUnitsText.text = "No. Of Units " + game.f.noUnits.ToString();
             game.f.avalUnits = game.f.avalUnits - 1;
             fAvalUnitsText.text = "Available units  " + game.f.avalUnits.ToString();
             game.money = game.money + 50;
             moneyText.text = "£" + game.money.ToString();
         }
 
         if (game.f.noUnits < 0)
         {
             game.f.noUnits = 0;
             game.f.avalUnits = 0;
             noUnitsCanvas.enabled = true;
         }
     }
 
     public void rPlus()
     {
         if (game.money >= 50)
         {
             game.r.noUnits = game.r.noUnits + 1;
             rUnitsText.text = "No. Of Units " + game.r.noUnits.ToString();
             game.r.avalUnits = game.r.avalUnits + 1;
             rAvalUnitsText.text = "Available units  " + game.r.avalUnits.ToString();
             game.money = game.money - 50;
             moneyText.text = "£" + game.money.ToString();
         }
 
         else
         {
             noMoneyCanvas.enabled = true;
         }
     }
 
     public void rMinus()
     {
         if (game.r.noUnits >= 1)
         {
             game.r.noUnits = game.r.noUnits - 1;
             rUnitsText.text = "No. Of Units " + game.r.noUnits.ToString();
             game.r.avalUnits = game.r.avalUnits - 1;
             rAvalUnitsText.text = "Available units  " + game.r.avalUnits.ToString();
             game.money = game.money + 50;
             moneyText.text = "£" + game.money.ToString();
         }
 
         if (game.r.noUnits < 0)
         {
             game.r.noUnits = 0;
             game.r.avalUnits = 0;
             noUnitsCanvas.enabled = true;
         }
     }
 
     public void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/pss_data.dat");
 
         PlayerData data = new PlayerData();
         data.money = game.money;
         data.time = game.time;
         data.missionsComplete = game.missionsComplete;
         data.gameLost = game.gameLost;
         data.tutorials = game.tutorials;
         data.p_noUnits = game.p.noUnits;
         data.p_avalUnits = game.p.avalUnits;
         data.p_fundingLevel = game.p.fundingLevel;
         data.p_level = game.p.level;
         data.f_noUnits = game.f.noUnits;
         data.f_avalUnits = game.f.avalUnits;
         data.f_fundingLevel = game.f.fundingLevel;
         data.f_level = game.f.level;
         data.r_noUnits = game.r.noUnits;
         data.r_avalUnits = game.r.avalUnits;
         data.r_fundingLevel = game.r.fundingLevel;
         data.r_level = game.r.level;
 
         bf.Serialize(file, data);
         file.Close();
     }
 
     public void Load()
     {
         if (File.Exists(Application.persistentDataPath + "/pss_data.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/pss_data.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize(file);
             file.Close();
 
             game.money = data.money;
             game.date = data.date;
             game.missionsComplete = data.missionsComplete;
             game.gameLost = data.gameLost;
             game.tutorials = data.tutorials;
 
             game.p.noUnits = data.p_noUnits;
             game.p.avalUnits = data.p_avalUnits;
             game.p.fundingLevel = data.p_fundingLevel;
             game.p.level = data.p_level;
 
             game.f.noUnits = data.f_noUnits;
             game.f.avalUnits = data.f_avalUnits;
             game.f.fundingLevel = data.f_fundingLevel;
             game.f.level = data.f_level;
 
             game.r.noUnits = data.r_noUnits;
             game.r.avalUnits = data.r_avalUnits;
             game.r.fundingLevel = data.r_fundingLevel;
             game.r.level = data.r_level;
 
         }
 
     }
 
     [Serializable]
     class PlayerData
     {
         public int money;
         public string time;
         public string date;
         public int missionsComplete;
         public bool gameLost;
         public bool tutorials;
 
         public int p_noUnits;
         public int p_avalUnits;
         public string p_fundingLevel;
         public int p_level;
 
         public int f_noUnits;
         public int f_avalUnits;
         public string f_fundingLevel;
         public int f_level;
 
         public int r_noUnits;
         public int r_avalUnits;
         public string r_fundingLevel;
         public int r_level;
     }
 
 
 
     // Update is called once per frame
     void Update()
     {
         Canvas.ForceUpdateCanvases();
     }
 }

Comment
Add comment · Show 1
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 mitchellson97 · Jan 14, 2016 at 03:37 PM 0
Share

I added more debugging to the code so it now prints game.money and data.money upon saving and loading and these match up (both 0 or both 100 dependant on the action I do) but once the load is finished and i save straight after (not pressing anything to change the money variable, it sets itself to 50 once again (both within game and data)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mitchellson97 · Jan 14, 2016 at 02:11 PM

Just to help out a little more I added debug code at the end of save and load both appear in my debugger as being complete.

I have attached my serialized file (purely because it doesn't look right to me)

alt text


2016-01-14.png (23.4 kB)
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

39 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

Related Questions

Setting components variables with another component 0 Answers

Can't figure out how to serialize my save data... 1 Answer

Serializing name of the current level 1 Answer

Looking for a more efficient method of saving for mobile game. 0 Answers

How to Serialize or Save a List of GameObjects 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