Deserialization problems or newbie error

There is such a system of saving. The bottom line is to reduce the number of save files. I tried to use an add-on to an existing sav file. The file is being created. But when it comes time to load data from it, an error appears in Unity
InvalidCastException: Specified cast is not valid. SaveGameToM.LoadT () (at Assets / Script / SaveGameToM.cs: 111)
That is, as I understand it, it cannot deserialize. What’s my mistake?

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


public class SaveGameToM : MonoBehaviour
{
    public GameObject Cube1Pref;
    public GameObject Triangle1Pref;



    [System.Serializable]
    public struct myVector3
    {
        public float x, y, z;
    }

    [System.Serializable]
    public struct myVector4
    {
        public float j, k, l;
    }



    [SerializeField] public List<myVector3> ObjectsPositions = new List<myVector3>();
    [SerializeField] public List<myVector4> ObjectsPositionsT = new List<myVector4>();
   


    public void Start()
    {
        if (File.Exists(Application.persistentDataPath + "/saves/save.sav"))
        {
            BackMyCubes();
            BackMyTriangles();
        }
    }

    public void BackMyCubes()
    {
        ObjectsPositions = Load();

        foreach (var pos in ObjectsPositions)
        {
            GameObject i = Instantiate(Cube1Pref, new Vector3(pos.x, pos.y, pos.z), Quaternion.identity);
        }
        ObjectsPositions.Clear();
    }

    public void BackMyTriangles()
    {
        ObjectsPositionsT = LoadT();

        foreach (var posT in ObjectsPositionsT)
        {
            GameObject t = Instantiate(Triangle1Pref, new Vector4(posT.j, posT.k, posT.l), Quaternion.identity);
        }
        ObjectsPositionsT.Clear();
    }

    public void ManualSaving()
    {
        foreach (var obj in GameObject.FindGameObjectsWithTag("Cube1"))
        {
            myVector3 i = new myVector3();
            i.x = obj.transform.position.x;
            i.y = obj.transform.position.y;
            i.z = obj.transform.position.z;
          
            ObjectsPositions.Add(i);
        }
        SaveCu(ObjectsPositions);
    }

    public void ManualSavingT()
    {
        foreach (var objT in GameObject.FindGameObjectsWithTag("Triangle1"))
        {
            myVector4 t = new myVector4();
            t.j = objT.transform.position.x;
            t.k = objT.transform.position.y;
            t.l = objT.transform.position.z;

            ObjectsPositionsT.Add(t);
        }
        SaveT(ObjectsPositionsT);
    }

    public List<myVector3> Load()
    {
        List<myVector3> objects;
       
        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream stream = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Open))
        {
            objects = (List<myVector3>)formatter.Deserialize(stream);
        }
        return objects;
    }

    public List<myVector4> LoadT()
    {
        List<myVector4> objectsT;
        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream streamT = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Open))
        {
            objectsT = (List<myVector4>)formatter.Deserialize(streamT);
        }
        return objectsT;
    }

    void SaveCu(List<myVector3> positions)
    {

        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream stream = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Create))
        {
            formatter.Serialize(stream, positions);
            stream.Close();
        }
        positions.Clear();
    }

    void SaveT(List<myVector4> positionsT)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream streamT = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Append))
        {
            formatter.Serialize(streamT, positionsT);
            streamT.Close();
        }
        positionsT.Clear();

    }
}

Don’t 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.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide

Load/Save steps:

https://discussions.unity.com/t/799896/4

1 Like

It’s not the save method, I’m wondering where the error was.