//here is the line where I initially attempt to convert between JSON and c# object
allchar = JsonUtility.FromJson(File.ReadAllText(Application.dataPath + “/Files/Characters.json”));
//then if I tried to get any data from this object, for example:
Debug.Log(allchar.allcharacter[0].name);
//then unity would give me a “NullReferenceException: Object reference not set to an instance of an object” on that line
// here's the class I'm trying to convert the JSON to
private class charactersjson
{
public characterjson[] allcharacter;
}
//here's the class referenced in the class above
private class characterjson
{
public string name;
public string[] Dict1;
public string[] Dict2;
public string[] Dict3;
}
//finally here is the example JSON file that I've been using(using it for tests ignore the dialogue lol)
{
"allcharacter": [
{
"name": "terry",
"Dict1": [ "describe Terry", "tall man with fake jordans", "talk to terry", "get the fuck out of here" ],
"Dict2": [],
"Dict3": []
}
]
}
//Another thing to note is that:
Debug.Log(File.ReadAllText(Application.dataPath + "/Files/Characters.json"));
//this would actually receive the data from the file and would display the text in the file as a debug message in unity
//I got this same conversion method to work for a different class that didn't have other JSON objects inside an array, which I'll show below:
currentsavefile = JsonUtility.FromJson<savefile>(File.ReadAllText(Application.dataPath + $"/Saves/savefile{PlayerPrefs.GetInt("savefile")}.json"));
//then the below code would actually give me the data from the file:
Debug.Log(currentlocation);
// and below is the class made for the object
private class savefile
{
public string[] inventory;
public string location;
public int [] coordinate;
public int money;
public string[] everycharcurrdict;
public string[] storydecisions;
}