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
1
Question by adovehv · Sep 29, 2019 at 01:17 PM · binaryformatterbinarybinarywriter

Problem with my binary files

Hi, i'm saving data in binary files, is my first time doing this, and i have a problem, when i compile the game, in my computer save and load data correctly, but in other compuers not work. I don't know whats happen, someone can help me?, thanks so much.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 
 [Serializable]
 public class SaveData
 {
     public bool[] isActive;
     public int[] highScores;
     public int[] stars;
 }
 
 public class GameData : MonoBehaviour
 {
     public static GameData gameData;
     public SaveData saveData;
 
     // Start is called before the first frame update
     void Awake()
     {
         if (gameData == null)
         {
             DontDestroyOnLoad(this.gameObject);
             gameData = this;
         }
         else
         {
             Destroy(this.gameObject);
         }
         Load();
     }
 
     void Start()
     {
         
     }
 
     public void Save()
     {
         //Create a binary formatter which can binary files
         BinaryFormatter formatter = new BinaryFormatter();
 
         //Create a route from the program to the file
         FileStream file = File.Open(Application.persistentDataPath + "/GameData.dat", FileMode.Create);
 
         //Create a copy of my save data
         SaveData data = new SaveData();
         data = saveData;
 
         //Actually save the data in the file
         formatter.Serialize(file, data);
 
         //close the data stream
         file.Close();
 
         Debug.Log("Saved Data");
     }
 
     public void Load()
     {
         //Check if the save gmae file exists
         if (File.Exists(Application.persistentDataPath + "/GameData.dat"))
         {
             //Create a Binary Formatter
             BinaryFormatter formatter = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/GameData.dat", FileMode.Open);
             saveData = formatter.Deserialize(file) as SaveData;
             file.Close();
             Debug.Log("Loaded Data");
         }
     }
 
     void OnApplicationQuit()
     {
         Save();
     }
 
     void OnDisable()
     {
         Save();
     }
 }
 


Comment
Add comment · Show 13
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 Mouton · Sep 29, 2019 at 02:18 PM 0
Share

On what systems/versions are you running your application ? Do you have error logs when trying to load a file from a different computer ?

avatar image adovehv Mouton · Sep 29, 2019 at 02:39 PM 0
Share

In window 10, they are two similar pc and, no, i don't have nothing error logs. In my computer work fine, but in the my friend computer and in my laptop not work.

avatar image Mouton adovehv · Sep 29, 2019 at 02:43 PM 0
Share

At least, do you see the Loaded Data debug message when trying to load the file?

Show more comments

1 Reply

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

Answer by Bunny83 · Sep 29, 2019 at 10:32 PM

That's why you should never mess with any kind of path strings manually by concatenating string fragments. You should always use System.IO.Path.Combine(). It should work properly on on all systems (linux, mac, android == forward slash, windows == backslash). It also should deal with potential leading or trailing slashes automatically.


So in your case:

 string fileName = System.IO.Path.Combine(Application.persistentDataPath, "GameData.dat");


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 adovehv · Sep 30, 2019 at 11:32 AM 0
Share

Thanks! :)

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

111 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

Related Questions

Binary File Does not work. 0 Answers

Reading and writing binary file, file unexpectedly large? 1 Answer

[Serialization] Multiple classes into one file? 1 Answer

SerializationException: End of Stream encountered before parsing was completed. 0 Answers

How to delete created Binary files 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