When loading saved data show error : SerializationException: End of Stream encountered before parsing was completed.

When loading data it show error SerializationException: End of Stream encountered before parsing was completed.
This is my Save script

public static void SaveInventory(GameManager gameManager)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/inventory.bin";
        FileStream stream = new FileStream(path, FileMode.Create);

        Inventory data = new Inventory(gameManager);

        formatter.Serialize(stream, data);
        stream.Close();
    }
    public static Inventory LoadInventory()
    {
        string path = Application.persistentDataPath + "/inventory.bin";

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

            Inventory data = formatter.Deserialize(stream) as Inventory;
            stream.Close();

            return data;
        }
        else
        {
            Debug.LogError("Save file not found in " + path);
            return null;
        }
    }

and this is my serialize script

[System.Serializable]
public class Inventory
{
    public List<Item> items;
    public Inventory(GameManager gameManager)
    {
        items = gameManager.items;
    }
}

and this is item class

[CreateAssetMenu(menuName = "Item", fileName = "New Item")]
public class Item : ScriptableObject
{
    public string itemName;
    public int itemPrice;
    public Sprite itemSprite;
}

You can not serialize ScriptableObject derived classes with the BinaryFormatter. ScriptableObjcts can only be created / recreated with ScriptableObject.CreateInstance.

Apart from that you should not use the BinaryFormatter as it’s insecure.

Why is your Item class a ScriptableObject? You would use ScriptableObjects for data you want to serialized in the Unity editor and ship with your game. ScriptableObjects can only be serialized with Unity’s JsonUtility anc can only be serialized as top object. Also one instance of your Item class could only be deserialized with FromJsonOverwrite that is used on an existing Item instance. So this is quite impractical.

It seems like your Item classes are actually configuration data that should not be serialized external but is actually part of your game. What you do want to serialize is the “logical link” to those items. The most logical way in your case would be to create a seperate save data class that has a list of string values that hold the name of the items. After deserialization you have to recreate the links to your actual item assets. So you have to search for the right Item instance by name. The easiest way to do this is to use a Dictionary<string, Item> which you can create / fill at startup with all your Items in your game. Of course that requires that each Item has a unique name.