Timeline for Reading JSON nested objects with JsonUtility
Current License: CC BY-SA 4.0
10 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 9, 2023 at 10:22 | comment | added | Basic |
You're telling Unity "I'll make sure that value is populated as needed". Have a read of this docs.unity3d.com/Manual/script-Serialization.html, especially "How Unity uses serialization", it mentions all the times Unity might serialize the value and reload it. One example of this is -say- "hot reloading" where the editor recompiles a script and updates it dynamically. Objects can be serialised and restored. Unity won't make sure your locations are restored, that's now your responsibility [if you want it to support hot reloading in the editor].
|
|
Sep 8, 2023 at 13:20 | comment | added | misaochan |
Hey, thanks a lot for the clarification! For some reason changing it to a property didn't solve the issue for me (still got the same error), but using [NonSerialized] to make it not serializable worked. :) Just checking: The only downside of this approach is that I can't change the values in the editor, right?
|
|
Sep 8, 2023 at 12:14 | comment | added | Basic |
If you don't want Unity serializing that field (you'll always load the value yourself from Json any time the scene/object is reloaded) and don't mind it not showing in the editor, you can change the field to a property ... public List<LocationSO> rootLocationList {get; set;} which you can reference in code, but not the editor. [In hindsight, this should've been an edit, not 3 comments... Sorry]
|
|
Sep 8, 2023 at 12:10 | comment | added | Basic | I suspect unity's built-in serializer recursion limit of 10 is configurable, but have never had to change it myself, so you would probably benefit by posting that as a new question. The key detail is ... It's nothing to do with loading from JSON. If you had that same property and manually added a tree nesting 11 levels deep, you'd get the same issue. | |
Sep 8, 2023 at 12:08 | comment | added | Basic |
You're dealing with conflicting serialisation strategies. Unity regularly serialises everything in a scene/attached to game objects (basically anything you wire up in the editor). It does this when saving/loading/playing/switching scenes, editing prefabs and a lot of other scenarios, and it does it using Unity's built in serializer (into a binary format for efficiency). When you expose the localRootLocationList in the editor, Unity sees the property and tries to serialise it.
|
|
Sep 8, 2023 at 8:51 | comment | added | misaochan |
Thanks! This works when I keep rootLocationList as a local variable, but as soon as I try to turn it into a publicly-accessible field (i.e. public List<LocationSO> rootLocationList = new List<LocationSO>(); , Unity throws a warning: Serialization depth limit 10 exceeded at 'LocationSO.locations'. There may be an object composition cycle in one or more of your serialized classes. Am I doing something wrong?
|
|
Sep 1, 2023 at 9:02 | vote | accept | misaochan | ||
Sep 1, 2023 at 2:50 | history | edited | Basic | CC BY-SA 4.0 |
added 287 characters in body
|
Sep 1, 2023 at 2:43 | history | edited | Basic | CC BY-SA 4.0 |
added 49 characters in body
|
Sep 1, 2023 at 2:35 | history | answered | Basic | CC BY-SA 4.0 |