title: Unity Save Load Function: Using Application.persistentDataPath
body: <p>I'm a beginner Unity developer.
  I've made Save &amp; Load function using an asset that supports the storage function.
  It is a common way to save it by creating a file in the form of Json in the Application.personDataPath/ folder.</p>
  <p>The problem is that it works perfectly well in Unity Editor, but it doesn't when I build it as a complete game. When I look up in Application.persistantDataPath/, which is target folder, nothing is created.</p>
  <p>Is there a solution or a diagnostic method that you use often?</p>
  <p>​For more information, look at the code below.
  I've used Asset called 'Corgi Engine' that I've bought from the Unity Asset Store.</p>
  <p><strong>Setting the path for file</strong></p>
  <pre><code>/// the method to use when saving and loading files (has to be the same at both times of course)
  
  public static IMMSaveLoadManagerMethod saveLoadMethod = new MMSaveLoadManagerMethodBinary();
  
  /// the default top level folder the system will use to save the file
  
  private const string _baseFolderName = &quot;/MMData/&quot;;
  
  /// the name of the save folder if none is provided
  
  private const string _defaultFolderName = &quot;MMSaveLoadManager&quot;;
  
  ​
  
  /// &lt;summary&gt;
  
  /// Determines the save path to use when loading and saving a file based on a folder name.
  
  static string DetermineSavePath(string folderName = _defaultFolderName)
  
  {
  
  string savePath;
  
  // depending on the device we're on, we assemble the path
  
  if (Application.platform == RuntimePlatform.IPhonePlayer)
  
  {
  
  savePath = Application.persistentDataPath + _baseFolderName;
  
  } 
  
  else 
  
  {
  
  savePath = Application.persistentDataPath + _baseFolderName;
  
  }
  
  #if UNITY_EDITOR
  
  savePath = Application.dataPath + _baseFolderName;
  
  #endif
  
  ​
  
  savePath = savePath + folderName + &quot;/&quot;;
  
  return savePath;
  
  }
  </code></pre>
  <p><strong>Creating directory and saving</strong></p>
  <pre><code>/// &lt;summary&gt;
  
  /// Save the specified saveObject, fileName and foldername into a file on disk.
  
  public static void Save(object saveObject, string fileName, string foldername = _defaultFolderName)
  
  {
  
  string savePath = DetermineSavePath(foldername);
  
  string saveFileName = DetermineSaveFileName(fileName);
  
  // if the directory doesn't already exist, we create it
  
  if (!Directory.Exists(savePath))
  
  {
  
  Directory.CreateDirectory(savePath);
  
  }
  
  // we serialize and write our object into a file on disk
  
  ​
  
  FileStream saveFile = File.Create(savePath + saveFileName);
  
  ​
  
  saveLoadMethod.Save(saveObject, saveFile);
  
  saveFile.Close();
  
  }
  </code></pre>
  <p><strong>Converting to Json and Saving</strong></p>
  <pre><code>/// &lt;summary&gt;
  
  /// Saves the specified object at the specified location after converting it to json
  
  public void Save(object objectToSave, FileStream saveFile)
  
  {
  
  string json = JsonUtility.ToJson(objectToSave);
  
  // if you prefer using NewtonSoft's JSON lib uncomment the line below and commment the line above
  
  //string json = Newtonsoft.Json.JsonConvert.SerializeObject(objectToSave);
  
  StreamWriter streamWriter = new StreamWriter(saveFile);
  
  streamWriter.Write(json);
  
  streamWriter.Close();
  
  saveFile.Close();
  
  Debug.Log(objectToSave.GetType());
  
  }
  </code></pre>