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 *);*
  •  		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;*
    
  • }*
    }

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")