So, for my game I would like to be able to add new projectiles easily by reading a JSON file and it just isn't working for me. I've never really used JSON before so I'm new to it and I just keep confusing myself. Here is the code and an example of the JSON I'm working with (N.B. I'm working in C#):
public class JSONTest : MonoBehaviour {
{
[Serializable]
public class Root
{
public Projectile[] proj;
}
[Serializable]
public class Projectile {
public int ID;
public string name;
public float velocity;
public int bounces;
public bool explosive;
}
public static Root JsonConvert(string Json)
{
return JsonUtility.FromJson<Root>(Json);
}
}
// Start is called before the first frame update
void Start()
{
string jsonString = File.ReadAllText(Application.dataPath + "/JSON/Projectiles.json"); //reads the file into string
Debug.Log(jsonString);
Root test = Projectile.JsonConvert(jsonString);
Debug.Log(test.proj.Length);
}
}
JSON:
{
"Projectiles":[{
"ID":1,
"name":"Bullet",
"velocity":1.5,
"bounces":3,
"explosive":false
}, {
"ID":2,
"name":"Rocket",
"velocity":3,
"bounces":1,
"explosive":true
}]
}
Every time it runs, I get a NullReferenceException on the second Debug.Log().
Any help would be greatly appreciated