This is my script (or a piece of it):
#region Stats
private static string heroName;
[HideInInspector] public string PlayerName = heroName;
private static float speed;
[HideInInspector] public float PlayerSpeed = speed;
private static string runKey;
[HideInInspector]public string RunKey = runKey;
private static string crowchKey;
[HideInInspector] public string CrowchKey = crowchKey;
#endregion
public PlayerID save = new PlayerID(0, heroName, speed, crowchKey, runKey, false);
JsonData playerJson;
private string playerString;
private JsonData playerData;
private void Start ()
{
heroName = gameObject.name;
playerJson = JsonMapper.ToJson(save);
File.WriteAllText(Application.dataPath + "/MyAssets/Resources/Player.json", playerJson.ToString());
Debug.Log(playerJson);
if (Application.dataPath + "/MyAssets/Resources/Player.json" == null)
speed = 120000;
speed = float.Parse(playerData[0]["speed"].ToString());
playerString = File.ReadAllText(Application.dataPath + "/MyAssets/Resources/Player.json");
playerData = JsonMapper.ToObject(playerString);
speed = float.Parse(playerData[0]["speed"].ToString());
}
public class PlayerID
{
public int id;
public string name;
public float speed;
public string crowch;
public string run;
public bool invertV;
public PlayerID (int id, string name, float speed, string crowch, string run, bool invertV)
{
this.id = id;
this.name = name;
this.speed = speed;
this.crowch = crowch;
this.run = run;
this.invertV = invertV;
}
}
I’m actually trying to load a speed definition, and convert it to a float (in this case private static float speed), but I always get a JsonExeption:
JsonException: Max allowed object depth reached while trying to export from type System.Single
LitJson.JsonMapper.WriteValue (System.Object obj, LitJson.JsonWriter writer, Boolean writer_is_private, Int32 depth) //Just imagine thousands of lines like this at the console. That's my case.
LitJson.JsonMapper.ToJson (System.Object obj)
Player.Awake () (at Assets/MyAssets/Player/Scripts/Player.cs:56)
What the hell is this and why can’t the script just work as it should?
PS: I’m using LitJson to use .json scripting.
HinxLai
3
Download this version of litJson in github. Thanks to
@mviranyi
by the way.
I figured that it was cleaner and simpler to use Unity’s json support, take a look:
public class SaveSpeed: MonoBehaviour
{
private void Start ()
{
heroName = gameObject.name;
Load();
}
void AddSpeed()
{
galahad.speed += 1000;
Save();
speedUpgrade = false;
}
public void Save()
{
string newGalahad = JsonUtility.ToJson(galahad);
File.WriteAllText(path, newGalahad);
}
public void Load()
{
path = Application.streamingAssetsPath + "/Galahad.json";
jsonString = File.ReadAllText(path);
galahad = JsonUtility.FromJson<PlayerID>(jsonString);
{
}
[System.Serializable]
public class PlayerID
{
public int id;
public string name;
public float speed;
public bool InvertV;
public bool InvertH;
}
Hope this help someone to solve their problems!