- Home /
The question is answered, right answer was accepted
FileNotFoundException: Could not find file
I've tried to find an answer to this in many threads but I just can't figure out what I'm doing wrong. Sorry.
So im following a tutorial to learn how to make a database for my game (this one https://www.youtube.com/watch?v=x24t4DCxGh8) But when I try to run my game I get this error: FileNotFoundException: Could not find file "C:\Users\User\Documents\Unity Projects\GD\Assets\StreamingAssets\Items.json". System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:305) System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare) System.IO.File.OpenRead (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:363) System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamReader.cs:167) System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding) (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string,System.Text.Encoding) System.IO.File.ReadAllText (System.String path, System.Text.Encoding encoding) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:587) System.IO.File.ReadAllText (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:582)
the line of code causing the error is this one:
void Start() { itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json")); ContructItemDatabase(); Debug.Log(database[1].Title); }
I don't know what is happening, the file IS there. Any ideas?
Have you tried changing the folder name from "Unity Projects" to "Unity_Projects" or "UnityProjects" (something without a space)? Are you sure that you are checking the right folder? Do you get to the right folder if you paste "C:\Users\User\Documents\Unity Projects\GD\Assets\Strea$$anonymous$$gAssets" into the adress line of the windows explorer?
Yes, I just changed the folder name to "UnityProjects" but I get the same result and yes I get the right folder when I paste "C:\Users\User\Documents\UnityProjects\GD\Assets\Strea$$anonymous$$gAssets" into the adress line. Any other ideas? :(
Hmm, what happens if you execute a script with just
using UnityEngine;
using System.IO;
public class Test : $$anonymous$$onoBehaviour
{
void Start()
{
string path = "C:/Users/User/Documents/Unity Projects/GD/Assets/Strea$$anonymous$$gAssets/Items.json";
FileInfo fileInfo = new FileInfo(path);
Debug.Log("File exists? " + (fileInfo.Exists ? "Yes" : "No"));
if (fileInfo.Exists)
{
Debug.Log("Full name: " + fileInfo.FullName);
Debug.Log("Content: " + File.ReadAllText(path));
}
}
}
Weird idea: Did you turn off the display of known file extensions in the explorer? Is the filename probably "Items.json.txt"? What do you see in the properties (select the file and ALT+ENTER or select "properties" from context menu.)?
YES! The file extension was .txt ins$$anonymous$$d of .json, I changed the extension and now it works perfectly. Thank you!
Wow, nice bit of lateral thinking there, @ScaniX
Follow this Question
Related Questions
do something if a method cant be executed 1 Answer
Saving list of vector 3 using JSON 1 Answer
Issues saving file in iOS 0 Answers
How do I go about deserializing a json array? 3 Answers
How to protect JSON file game data? 3 Answers