0
\$\begingroup\$

I'm trying to serialize / deserialize a class that holds inventory items.

The code is attached below.

First I call AddOne() void a few times, then I call SaveFile(), then I call LoadFile().

In LoadFile() the following error occurs :

ex = "System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed.\r\n at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run () [0x00288] in <1f0c1ef1ad524c38bbc5536809c46b48>:0 \r\n at System.Runt...

Does anybody see my mistake?

Thank you for the help!

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

public enum Type
{
    undefined = 0,
    Pistol = 1,
    Magnum = 2,
    Grenade = 3,
    BlendGrenade = 4,
    FireGrenade = 5,
    PistolAmmo = 6,
    MagnumAmmo = 7,
    Rifle,
    RifleAmmo,
}

[System.Serializable]
public class Inventory : MonoBehaviour
{
    public class InventoryItem
    {
        public int Amount = 0;
        public int Row = 0;
        public int Col = 0;
        public int Rotation = 0;
        public Type ItemType = Type.undefined;
    }
    public List<InventoryItem> Items;

    public void Start()
    {
        Items = new List<InventoryItem>();
    }
    public void AddOne()
    {
        InventoryItem n = new InventoryItem();
        this.Items.Add(n);
    }
    public void SaveFile()
    {
        string destination = Application.persistentDataPath + "/save3.dat";

        if (File.Exists(destination))
        {
            File.Delete(destination);
        }
        using (Stream stream = File.Open(destination, FileMode.Create))
        {
            BinaryFormatter bin = new BinaryFormatter();
            bin.Serialize(stream, this.Items);
        }
    }

    public void LoadFile()
    {
        string destination = Application.persistentDataPath + "/save3.dat";

        using (Stream stream = File.Open(destination, FileMode.Open))
        {
            BinaryFormatter bin = new BinaryFormatter();

            try
            {
                this.Items = (List<InventoryItem>)bin.Deserialize(stream);
            }
            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
            }
        }
    }
}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ You save an array, but you read a list. \$\endgroup\$ Commented Jun 22, 2019 at 23:35
  • \$\begingroup\$ @S.TarıkÇetin Thank you. I saw this example on the net and just adopted it to my needs. According to your comment, I have changed the array to a List now. I have edited my post accordingly. However, the very same error occurs. \$\endgroup\$
    – tmighty
    Commented Jun 23, 2019 at 21:17

1 Answer 1

0
\$\begingroup\$

I found the error:

The Serializable attribute needs to be on the InventoryItem, not on Inventory.

The correct code looks like this:

public class Inventory : MonoBehaviour
{
    [System.Serializable]
    public class InventoryItem
    {

I found the error by adding Try Catch statements. Unfortunately, the Unity Console doesn't display C# runtime errors. At least I don't see them.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .