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;
}