Error Massage at the binary serilation

Hello guys I need help with my code, I can not fix the issue I don’t even find the issue in the code.
Here is the Error Message

ArgumentException: Name has invalid chars
System.IO.FileStream…ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at :0)
System.IO.FileStream…ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize) (at :0)
(wrapper remoting-invoke-with-check) System.IO.FileStream…ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
System.IO.File.Create (System.String path, System.Int32 bufferSize) (at :0)
System.IO.File.Create (System.String path) (at :0)
BinarySerializer.Save[T] (T data, System.String filename) (at Assets/BinarySerializer.cs:41)
GameDataManager.SavePlayerData () (at Assets/Scripts/GameDataManager.cs:50)
GameDataManager.AddCoins (System.Int32 amount) (at Assets/Scripts/GameDataManager.cs:28)
Player.OnCollisionEnter2D (UnityEngine.Collision2D other) (at Assets/Scripts/Player.cs:45)

using UnityEngine;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;



public class BinarySerializer
{
    static string folderName = "GameData";

    static string persistentDataPath = Application.persistentDataPath;
    static SurrogateSelector surrogateSelector = GetSurrogateSelector ();

  
    public static void Save <T> (T data, string filename)
    {
        if (IsSerializable <T> ()) {
            if (!Directory.Exists (GetDirectoryPath ()))
                Directory.CreateDirectory (GetDirectoryPath ());

            BinaryFormatter formatter = new BinaryFormatter ();

            formatter.SurrogateSelector = surrogateSelector;

            FileStream file = File.Create (GetFilePath (filename));

            formatter.Serialize (file, data);

            file.Close ();
        }
    }

   
    public static T Load<T> (string filename)
    {
        T data = System.Activator.CreateInstance <T> ();

        if (IsSerializable <T> ()) {
            if (HasSaved (filename)) {
              
                BinaryFormatter formatter = new BinaryFormatter ();

                formatter.SurrogateSelector = surrogateSelector;

                FileStream file = File.Open (GetFilePath (filename), FileMode.Open);

                data = (T)formatter.Deserialize (file);

                file.Close ();
            }
        }

        return data;
    }

    static bool IsSerializable<T> ()
    {
        bool isSerializable = typeof(T).IsSerializable;
        if (!isSerializable) {
            string type = typeof(T).ToString ();
            Debug.LogError (
                "Class <b><color=white>" + type + "</color></b> is not marked as Serializable, "
                + "make sure to add <b><color=white>[System.Serializable]</color></b> at the top of your " + type + " class."
            );
        }

        return isSerializable;
    }


  
    public static bool HasSaved (string filename)
    {
        return File.Exists (GetFilePath (filename));
    }

    static string GetDirectoryPath ()
    {
        return persistentDataPath + "/" + folderName;
    }

    static string GetFilePath (string filename)
    {
        return  GetDirectoryPath () + "/" + filename;
    }


   

    static SurrogateSelector GetSurrogateSelector ()
    {
        SurrogateSelector surrogateSelector = new SurrogateSelector ();

        Vector2_SS v2_ss = new Vector2_SS ();
        Vector3_SS v3_ss = new Vector3_SS ();
        Vector4_SS v4_ss = new Vector4_SS ();
        Color_SS co_ss = new Color_SS ();
        Quaternion_SS qu_ss = new Quaternion_SS ();

        surrogateSelector.AddSurrogate (typeof(Vector2), new StreamingContext (StreamingContextStates.All), v2_ss);
        surrogateSelector.AddSurrogate (typeof(Vector3), new StreamingContext (StreamingContextStates.All), v3_ss);
        surrogateSelector.AddSurrogate (typeof(Vector4), new StreamingContext (StreamingContextStates.All), v4_ss);
        surrogateSelector.AddSurrogate (typeof(Color), new StreamingContext (StreamingContextStates.All), co_ss);
        surrogateSelector.AddSurrogate (typeof(Quaternion), new StreamingContext (StreamingContextStates.All), qu_ss);

        return surrogateSelector;
    }

    class Vector2_SS: ISerializationSurrogate
    {
      
        public void GetObjectData (System.Object obj, SerializationInfo info, StreamingContext context)
        {
            Vector2 v2 = (Vector2)obj;
            info.AddValue ("x", v2.x);
            info.AddValue ("y", v2.y);
        }
      
        public System.Object SetObjectData (System.Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
        {
            Vector2 v2 = (Vector2)obj;

            v2.x = (float)info.GetValue ("x", typeof(float));
            v2.y = (float)info.GetValue ("y", typeof(float));

            obj = v2;
            return obj;
        }
    }

    class Vector3_SS: ISerializationSurrogate
    {
      
        public void GetObjectData (System.Object obj, SerializationInfo info, StreamingContext context)
        {
            Vector3 v3 = (Vector3)obj;
            info.AddValue ("x", v3.x);
            info.AddValue ("y", v3.y);
            info.AddValue ("z", v3.z);
        }
      
        public System.Object SetObjectData (System.Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
        {
            Vector3 v3 = (Vector3)obj;

            v3.x = (float)info.GetValue ("x", typeof(float));
            v3.y = (float)info.GetValue ("y", typeof(float));
            v3.z = (float)info.GetValue ("z", typeof(float));

            obj = v3;
            return obj;
        }
    }

    class Vector4_SS: ISerializationSurrogate
    {
      
        public void GetObjectData (System.Object obj, SerializationInfo info, StreamingContext context)
        {
            Vector4 v4 = (Vector4)obj;
            info.AddValue ("x", v4.x);
            info.AddValue ("y", v4.y);
            info.AddValue ("z", v4.z);
            info.AddValue ("w", v4.w);
        }
      
        public System.Object SetObjectData (System.Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
        {
            Vector4 v4 = (Vector4)obj;

            v4.x = (float)info.GetValue ("x", typeof(float));
            v4.y = (float)info.GetValue ("y", typeof(float));
            v4.z = (float)info.GetValue ("z", typeof(float));
            v4.w = (float)info.GetValue ("w", typeof(float));

            obj = v4;
            return obj;
        }

    }

    class Color_SS: ISerializationSurrogate
    {
       
        public void GetObjectData (System.Object obj, SerializationInfo info, StreamingContext context)
        {
            Color color = (Color)obj;
            info.AddValue ("r", color.r);
            info.AddValue ("g", color.g);
            info.AddValue ("b", color.b);
            info.AddValue ("a", color.a);
        }
       
        public System.Object SetObjectData (System.Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
        {
            Color color = (Color)obj;

            color.r = (float)info.GetValue ("r", typeof(float));
            color.g = (float)info.GetValue ("g", typeof(float));
            color.b = (float)info.GetValue ("b", typeof(float));
            color.a = (float)info.GetValue ("a", typeof(float));

            obj = color;
            return obj;
        }
    }

    class Quaternion_SS: ISerializationSurrogate
    {
     
        public void GetObjectData (System.Object obj, SerializationInfo info, StreamingContext context)
        {
            Quaternion qua = (Quaternion)obj;
            info.AddValue ("x", qua.x);
            info.AddValue ("y", qua.y);
            info.AddValue ("z", qua.z);
            info.AddValue ("w", qua.w);
        }
     
        public System.Object SetObjectData (System.Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
        {
            Quaternion qua = (Quaternion)obj;

            qua.x = (float)info.GetValue ("x", typeof(float));
            qua.y = (float)info.GetValue ("y", typeof(float));
            qua.z = (float)info.GetValue ("z", typeof(float));
            qua.w = (float)info.GetValue ("w", typeof(float));

            obj = qua;
            return obj;
        }
    }
}

You are apparently attempting to save your data into a file with a filename that is illegal under your computer’s file system because it contains invalid characters.

Thank you and how can i fix it? :slight_smile:

Whats the file name?

1 Like

You…change the filename to something that uses only legal characters?

Sorry, I’m not really understanding what part of this you are having trouble with.

I don’t know what i have to change to fix this error

The file name.

can i choose one or do i have to use a specific one? and do you mean the file name of the scrip or the “filename” in the code?

Change whatever you’re passing to the Save and Load function.

Is there the issue?

It’s possible, if there’s a space in the file name, typically file systems don’t like that. If there’s a special character, typically, a file system won’t like that.

What filename are you passing to save and load?

Do you mean this one?

static void LoadPlayerData()
    {
        playerData = BinarySerializer.Load <PlayerData> ("player-data.txt ");
        UnityEngine.Debug.Log ("< color = green >[PlayerData] Loaded.</ color > ");
    }

    static void SavePlayerData()
    {
        BinarySerializer.Save(playerData, "player-data.txt >");
        UnityEngine.Debug.Log ("< color = magenta >[PlayerData] Saved.</color> ");
    }

You are saving it with a chevron on the end. (>), just remove the chevron and it should be okay.

1 Like

Yeah it works thank you very much man you really helped me. It was clearly my fault sorry for wasting your time.

Not a problem. Glad it works.