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 AmazingDeveloper2 · Mar 19, 2016 at 04:10 PM · c#unity 5file-iofilestream

Can not save data to file and then load it in Unity3d

I want to save data of my game to file after starting the game, then after close/start game again I want to load data from that file. I tried to use this solution, but when I start and then stop simulation, I don't see file indicatorsInfo.dat, but Debug.Log() says, that it exists. Anyway it don't load the data, what is wrong?

alt text

 using UnityEngine;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class GAMEMANAGERFileIO : MonoBehaviour
 {
     void OnEnable()
     {        
         LoadFromFile();
         SaveToFile();
     }
 
     void OnDisable()
     {        
         SaveToFile();
     }
 
     public void SaveToFile()
     {
         GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
         GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();            
 
         BinaryFormatter binaryFormatter = new BinaryFormatter();
         FileStream fileStream = File.Create(Application.persistentDataPath + "/indicatorsInfo.dat"); // open file
         IndicatorsInfo indicatorsInfo = new IndicatorsInfo();
 
         // Initialise data of the class IndicatorsInfo
         //...
 
         // save data to the file. Serialize([to where we want to save], [what we want to save])
         binaryFormatter.Serialize(fileStream, indicatorsInfo);
         fileStream.Close();
         Debug.Log("saved"); // this works
     }
 
     public void LoadFromFile()
     {
         // check if file exisits before we will try to open it
         if(File.Exists(Application.persistentDataPath + "/indicatorsInfo.dat"))
         {
             GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
             GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();
 
             BinaryFormatter binaryFormatter = new BinaryFormatter();
             FileStream fileStream = File.Open(Application.persistentDataPath + "/indicatorsInfo.dat", FileMode.Open); // open file
             IndicatorsInfo indicatorsInfo = (IndicatorsInfo)binaryFormatter.Deserialize(fileStream); // read from file
             fileStream.Close(); // close file
 
             // Initialise local data with values from the class IndicatorsInfo
             //...
 
             Debug.Log("loaded"); // this works
         }
     }
 }
 
 // clear class with data, which we will store to file
 [Serializable]
 class IndicatorsInfo
 {
     //...
 }


1.jpg (36.8 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

2 Replies

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

Answer by MechanicalGaming · Mar 19, 2016 at 11:11 PM

Try putting the game on standalone and then trying it out. Start the game, gain some points of what ever, quit the game and then start it back up to see what has changed! Unity simulation doesn't load the file.

Comment
Add comment · Show 2 · 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 AmazingDeveloper2 · Mar 19, 2016 at 11:34 PM 0
Share

I did'n know about that, thanks. But it still don't work

avatar image MechanicalGaming AmazingDeveloper2 · Mar 20, 2016 at 12:41 PM 1
Share

Well, if that is your code right there that you are using then you haven't told it to save anything. If you want it to save something you must put a float in it and all of that. Here's a video explaining it all: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

Watch the whole thing! It helps tons!

avatar image
0

Answer by FireBoltStudios · Mar 21, 2016 at 09:56 AM

This is a script I have created that I use in many of my projects, it can save out to XML and Binary, and load. As long the class to be saved is not inheriting MonoBehaviour and uses primitive data types. very simple usage; for instance to load from a file the line would be.

MyClass myClass = Serializer.LoadXML(Path);

To save;

Serializer.SaveXML(Path,myClass);

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Xml;
 using System.Xml.Serialization;
 
 namespace Assets.Scripts.Utility
 {
     public class Serialiser
     {
         public static T Load<T>(string filename) where T : class
         {
             if (File.Exists(filename))
             {
                 using (Stream stream = File.OpenRead(filename))
                 {
                     BinaryFormatter formatter = new BinaryFormatter();
                     return formatter.Deserialize(stream) as T;
                 }
             }
             return null;
         }
 
         public static void Save<T>(string filename, T data) where T : class
         {
             using (Stream stream = File.Open(filename, FileMode.OpenOrCreate))
             {
                 BinaryFormatter formatter = new BinaryFormatter();
                 formatter.Serialize(stream, data);
             }
         }
 
         public static T LoadXML<T>(string filename) where T : class
         {
             if (File.Exists(filename))
             {
                 using (Stream stream = File.OpenRead(filename))
                 {
                     XmlSerializer serial = new XmlSerializer(typeof(T));
                     return serial.Deserialize(stream) as T;
                 }
             }
 
             return null;
         }
 
         public static void SaveXML<T>(string filename, T data) where T : class
         {
             using (Stream stream = File.Open(filename, FileMode.OpenOrCreate))
             {
                 XmlSerializer serial = new XmlSerializer(typeof (T));
                 serial.Serialize(stream, data);
             }
         }
     }
 }
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

130 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Serialization is working fine in Unity but not on Android 2 Answers

termination of xcode project due to memory error while running unity game 0 Answers

GameObject cant find definition for anything, 2 suggesteds,GameObject only has Equals and Reference Equals 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