• 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 · binarybinaryformatterbinarywriter

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to use binaryformatter with custom class in unity? 1 Answer

Saving and Loading in Binary corrupted save help 1 Answer

Format of .scene files etc 1 Answer

'Too many bytes in what should have been a 7 bit encoded Int32' and idk what I'm doing wrong... 1 Answer

Save multiple scriptableObjects in just one binary file 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges