This question was
closed Apr 16, 2017 at 03:22 PM by
Febre for the following reason:
The question is answered, right answer was accepted
Question by
Febre · Apr 16, 2017 at 02:16 PM ·
c#unity 5serializablefilestream
EndOfStreamException: Failed to read past end of stream.
I cant seem to find the problem. Have been searching for an answer on google with no luck. Any help would be appreciated. See code below.
public class PlayerData : MonoBehaviour {
public static List<GameObject> objectList = new List<GameObject>();
public GameObject plank;
void Start(){
LoadObjects();
}
public void SaveObjects(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/Objects.dat", FileMode.Create);
ObjectsClass data = new ObjectsClass ();
data.ObjectList = objectList;
bf.Serialize (file, data);
file.Close();
}
public void LoadObjects(){
if (File.Exists (Application.persistentDataPath + "/Objects.dat")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/Objects.dat", FileMode.Open);
ObjectsClass data = (ObjectsClass)bf.Deserialize (file);
file.Close ();
objectList = data.ObjectList;
} else {
objectList.Add (plank);
}
}
}
[Serializable]
class ObjectsClass {
public List<GameObject> ObjectList;
}
EDIT: The problem was that GameObjects are not serializable.
Comment
Follow this Question
Related Questions
Cannot implicitly convert type `int' 1 Answer
Where does the local WWW path point on mac builds? 0 Answers
RAW image won't show on UI 0 Answers
Stuck With this codes. 1 Answer