Unity not saving a JSON file

I’m working on an application that records some information and stores the data in a serialized JSON file with the following code:

string DatabasePath = $"{Application.streamingAssetsPath}/HardPoints/";
string FileName = "HardPointData.json";
if (!Directory.Exists(DatabasePath)) Directory.CreateDirectory(DatabasePath);
if (File.Exists($"{DatabasePath}{FileName}")) File.Create($"{DatabasePath}{FileName}");
File.WriteAllText($"{DatabasePath}{FileName}", JsonConvert.SerializeObject(JSONData));
AssetDatabase.Refresh();

Despite adding the AssetDatabase.Refresh after saving the file, the file results as empty. While the application is running in Play mode within the editor the file and the data inside exist but the moment I stop the editor all of the file’s data vanishes.

Can someone tell me what I’m doign wrong? Is there some exception for the streamingAssetsPath? What am I missing?

Did you mean to write:

if (!File.Exists($"{DatabasePath}{FileName}")) File.Create($"{DatabasePath}{FileName}");


Notice the !

I have never used the Application.streamingAssetsPath but I try to answer anyway. It seams files written to the path is not kept between runs according to the doc: Unity - Scripting API: Application.streamingAssetsPath

I suggest that you use Application.persistentDataPath to store files that will be kept. Not sure if it works on all platforms though.