- Home /
Serialization Location
I'm trying to serialize an object holding an array of lists of other game objects that hold game objects and integers for timing (probably not important but that's that). When reading up on serialization, it used Application.persistentDataPath as a place to save the serialized data, however when I use it, I get an error that the location doesn't exist. Should I actually be using Application.persistentDataPath to save the info or somewhere else ?
Thanks.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Level_Creator : MonoBehaviour {
public static List<Control_Object>[] newLvl = new List<Control_Object>[150];
public static Game_Array_Object lvl;
void Start ()
{
GameObject[] allPeons = GameObject.FindGameObjectsWithTag ("Peon");
for (int j = 0; j < 10; j++)
{
newLvl [j] = new List<Control_Object> ();
for (int i = 0; i < (j*10 + 10); i++) {
Control_Object peon = new Control_Object (i * 100 + 100, allPeons [i]);
newLvl [j].Add (peon);
}
}
Debug.Log("Level Created!");
lvl = new Game_Array_Object (newLvl);
SaveLvl (0);
Debug.Log ("Level Saved!");
}
public static void SaveLvl(int s)
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/realms/realm_" + s.ToString() + ".realm");
bf.Serialize (file, Level_Creator.lvl);
file.Close ();
}
public static List<Control_Object>[] LoadLvl(int s)
{
if(File.Exists(Application.persistentDataPath + "/realms/realm_" + s.ToString() + ".realm"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/realms/realm_" + s.ToString() + ".realm", FileMode.Open);
List<Control_Object>[] temp = (List<Control_Object>[])bf.Deserialize (file);
file.Close ();
return temp;
}
return null;
}
}
Answer by QuiZr · Jul 17, 2016 at 03:06 PM
You shouldn't manually combine paths (i.e. Application.persistentDataPath + "/realms"). You should use:
System.IO.Path.Combine(Application.persistentDataPath, "realms", "realm_" + s.ToString() + ".realm")
While that's good to know and I have implemented that now, it does not solve my problem. The data path still does not exist.
Your answer
Follow this Question
Related Questions
I am getting a serialization exception error when trying to save and load in Unity? 2 Answers
Saving and Loading Various Things 1 Answer
Save/Load Animation State of Instantiated Prefabs 0 Answers
How to deserialize after I've added more members to a class 0 Answers
Serializing / deserializing large lists 0 Answers