So I’m trying to implement shop system to my game and I decided to use Json to store data about what skins has player already bought. I’m using the code below to Load & Save data into my .json file called “file.json”:
public class gameDataSaver : MonoBehaviour
{
public Skins skins;
public void LoadData(){
skins = JsonUtility.FromJson<Skins>(File.ReadAllText(Application.streamingAssetsPath + "/file.json"));
}
public void SaveData(){
File.WriteAllText(Application.streamingAssetsPath + "/file.json", JsonUtility.ToJson(skins));
}
[System.Serializable]
public class Skins{
public bool[] isPurchased;
}
}
To use Json I created “StreamingAssets” in my project assets and then created a file called “file.json”. When running the app, Unity gives me an error:
fileNotFoundException: Could not find file "C:\Users\user\TouchFollower\Assets\StreamingAssets\file.json"
I tried to check, if Unity sees the file by using the code below:
private string path;
private string jsonFileName = "file.json";
private void Start() {
path = Path.Combine(Application.streamingAssetsPath, jsonFileName);
Debug.Log("Path of streamingAssets: " + Application.streamingAssetsPath);
Debug.Log("Path of _path variable: " + path);
if(File.Exists(path) == false){
Debug.Log("File not found");
}else{
Debug.Log("File found");
}
}
But everytime it only returns me “File not found”. The full output is:
Path of streamingAssets: C:/Users/user/TouchFollower/Assets/StreamingAssets
Path of _path variable: C:/Users/user/TouchFollower/Assets/StreamingAssets\file.json
File not found
I noticed, that when Unity shows me FileNotFoundException, the path uses "" sign, but when displaying Debug.Log, the path is shown with “/” sign. Could that be a problem?