- Home /
Unity doesn't see files inside StreamingAssets folder
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?
C:/Users/user/TouchFollower/
is a very unusual place to hold your project files. Unity might not have the necessary permissions to always write/read files there. Try moving your project somewhere less ambivalent like D:/Projects/TouchFollower/
Thanks for the reply! Sadly, I don't have any other disks to put my project to. I tried to move my TouchFollower projects in any other folders, but it didn't work
Add these two lines:
if( !File.Exists(path) ) File.WriteAllText(path,"file contents");
Debug.Log($"File exists: {(File.Exists(path)?"yes":"no")} (path:{path})");
And compare location of this newly created file with location where you expected it to be.
The code returned: File exists: yes (path:C:/Users/user/TouchFollower/Assets/Strea$$anonymous$$gAssets\file.json). I went to the path and a new file appeared. I noticed, that Unity now sees the file that your code created. But my question is: why doesn't Unity still see my file.json? Maybe the problem is that I created my json file as a simple .txt file and replaced its name with "file.json"?
Answer by goodgamershow · Jun 06, 2021 at 03:15 PM
Solution was quite simple: My OS sets any created .txt file back to .txt extension, even if I override the extension. After changing this setting everything started to work properly. Big thanks to @andrew-lukasik
Your answer
Follow this Question
Related Questions
Streaming Assets android 0 Answers
Converting from SQLite Database to JSON 2 Answers
Parse JSON Data with JSON Utility to Dictionary object 1 Answer
can't find json file on android ? 1 Answer
Modifying Json file values 2 Answers