- Home /
Unity build cant find serialized file after restarting
Hi! I'm creating a 2D Sandbox Game and i'm currently trying to add saving. I've Pretty much made it work. I'm using a serialized file to save the Players inventory and all the blocks and their positions. I serialize 7 lists that hold the blocks ID, Number, X position, and Y position, and the Inventory Items Slot, Number, and ID. But when I tested saving in a build, and try to load the serialized file after restarting the build, it won't load the file. I searched on Google for an answer and I think it has to do with serializing lists.
Here's my code for saving and loading called SaveLoad.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public static class SaveLoad {
public static Game savedGame;
public static void Save () {
savedGame = Game.current;
if (Directory.Exists(Application.persistentDataPath + "/Saves")) {
}
else {
Directory.CreateDirectory(Application.persistentDataPath + "/Saves");
}
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/Saves/savedGame.save");
bf.Serialize(file, SaveLoad.savedGame);
file.Close();
}
public static void Load () {
if (File.Exists(Application.persistentDataPath + "/Saves/savedGame.save")) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/Saves/savedGame.save", FileMode.Open);
SaveLoad.savedGame = (Game)bf.Deserialize(file);
Game.current.BlocksX.Clear();
Game.current.BlocksY.Clear();
Game.current.BlockNums.Clear();
Game.current.IDs.Clear();
Game.current.InvIDs.Clear();
Game.current.Slots.Clear();
Game.current.ItemNums.Clear();
file.Close();
}
}
}
if you need more Info just ask.
What-s the error message? Did you insert Debug.Log lines to check if the file was found? (De)Serializing Lists and other collections shouldn't be the problem.
I added a debug message and it does find the file. but when i try to get the count of any list i get an object reference not set to an instance of an object error;
Your answer
Follow this Question
Related Questions
SQLite and Lists in C# 1 Answer
Serialization Error 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers