Save arrays by Binary?

Hi,
I’m trying to figure out a away to save/load my arrays using Binary method. My problem here I can’t save because an error.

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

[System.Serializable]
public class Replier
{
    public string[] PlayerName = new string[5];
    public int[] Score = new int[5];
    public float[] time = new float [5];
    private Replier datas1;

    public Replier(Replier datas1)
    {
        this.datas1 = datas1;
    }
}


public class scenario : MonoBehaviour
{
    public int Person;
    public Replier[] Characters = new Replier[5];

    public void savef()
    {
// error - There is no argument given that corresponds to the required formal parameter 'datas1' of 'scenario.SaveData(Replier)'
        SaveData();
    }
    public void loadf()
    {
        LoadData();
    }

    //-----------
    public static void SaveData(Replier datas1)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/GameSaveListArray";

        FileStream stream = new FileStream(path, FileMode.Create);

        Replier charData = new Replier(datas1);

        formatter.Serialize(stream, charData);
        print("Save File");
        stream.Close();
    }


    public static Replier LoadData()
    {
        string path = Application.persistentDataPath + "/GameSaveListArray";

        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            Replier datas1 = formatter.Deserialize(stream) as Replier;

            stream.Close();
            print("Load save file" + path);
            return datas1;
        }
        else
        {
            Debug.LogError("Error: Save file not found in " + path);
            return null;
        }
    }

What part of the error on line 29 is ambiguous??

The error has nothing to do with binary formatters. It’s basic language:

Line 38 defines SaveData as taking an argument.

Line 30 calls it without an argument.

Fix that.

Keep in mind the Binary formatter is extremely deprecated and dangerous:

Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

Load/Save steps:

An excellent discussion of loading/saving in Unity3D by Xarbrough:

Loading/Saving ScriptableObjects by a proxy identifier such as name:

When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls new to make one, it cannot make the native engine portion of the object.

Instead you must first create the MonoBehaviour using AddComponent() on a GameObject instance, or use ScriptableObject.CreateInstance() to make your SO, then use the appropriate JSON “populate object” call to fill in its public fields.

If you want to use PlayerPrefs to save your game, it’s always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

2 Likes