Problem with loading a GameObject from file

I keep getting this error whenever I run my code. I am new to such operations and I can not find the cause of the problem.

XmlException: Unexpected end of file.
System.Xml.EncodingStreamWrapper.ReadBOMEncoding (System.Boolean notOutOfBand)

This code saves GameObject “map”

public void SaveMap()
    {
        FileStream file = File.Create(Application.persistentDataPath
                     + "/" + mapSaveNameInputField.GetComponent<InputField>().text + ".dat");
        SaveData data = new SaveData();
        data.savedMap = map;
        DataContractSerializer bf = new DataContractSerializer(data.GetType());
        MemoryStream streamer = new MemoryStream();
        bf.WriteObject(streamer, data);
        streamer.Seek(0, SeekOrigin.Begin);
        file.Write(streamer.GetBuffer(), 0, streamer.GetBuffer().Length);
        file.Close();
        Debug.Log("Saved");
    }

This code loads GameObject “map”
The exception shows up whenever LoadMap() is called

public void LoadMap()
    {
        if (File.Exists(Application.persistentDataPath
                       + "/" + mapLoadNameInputField.GetComponent<InputField>().text + ".dat"))
        {
            FileStream file = File.Create(Application.persistentDataPath
                     + "/" + mapSaveNameInputField.GetComponent<InputField>().text + ".dat");
            DataContractSerializer bf = new DataContractSerializer(typeof(GameObject));
            SaveData data = (SaveData)bf.ReadObject(file);
            map = data.savedMap;
            MemoryStream streamer = new MemoryStream();
            file.Close();
            Debug.Log("Loaded");
        }else
        {
            Debug.Log("No such map");
        }
}
[DataContract]
class SaveData
{
    [DataMember]
    private GameObject _savedMap;

    public GameObject savedMap
    {
        get { return _savedMap; }
        set { _savedMap = value; }
    }
}

You’re overwriting file in LoadMap() with File.Create.
Change it to File.Open/File.OpenRead.
Also you’re deserializing GameObject type instead of SaveData type.

What are you expecting to happen here? You are going to have a really hard time if you’re trying to directly serialize an entire GameObject. Are you expecting the whole thing to get serialized along with all components etc? That’s not going to work. You need to pick the bits of data important to your game and directly save/load those.

Thanks for the replies! Despite the advice I got, I still get the same exception.
This is the actual code:

 public void LoadMap()
    {
        if (File.Exists(Application.persistentDataPath
                       + "/" + mapLoadNameInputField.GetComponent<InputField>().text + ".dat"))
        {
            FileStream file = File.OpenRead(Application.persistentDataPath
                     + "/" + mapSaveNameInputField.GetComponent<InputField>().text + ".dat");
            DataContractSerializer bf = new DataContractSerializer(typeof(SaveData));
            SaveData data = (SaveData)bf.ReadObject(file);
            map = data.savedMap;


            MemoryStream streamer = new MemoryStream();
            file.Close();
            Debug.Log("Loaded");
        }else
        {
            Debug.Log("No such map");
        }
}

I did some more testing and it seems that Unity basicly does not support saving GameObjects. Thanks for the help anyways :slight_smile: