Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Igor_Vasiak · Apr 26, 2017 at 11:08 PM · scripting problemsaveloadjsonlitjson

LitJson load float

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Igor_Vasiak · Apr 27, 2017 at 10:23 AM

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by HinxLai · Sep 18, 2018 at 03:47 AM

Download this version of litJson in github. Thanks to @mviranyi by the way.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Igor_Vasiak · Sep 18, 2018 at 05:06 PM 0
Share

Wow, I totally forgot about this! Nowadays I wouldn't use LitJson - or any other .json library, actually. But that's definately something to consider for ones who'd preffer it. Thanks! Each and every answer has its value since no one thinks the same way and everyone can contribute with a different approach to the same problem.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

98 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SimpleJSON array problem 0 Answers

Why my Serializable class isn't receiving values from a Generic .json loader? 2 Answers

How to store a nullable int into a .json file? 1 Answer

How to save and load any data type? 1 Answer

JSON - Load Subset of Files Based on Key in File 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges