Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

JSON loads ScriptableObjects incorrectly

Discussion in 'Editor & General Support' started by piscopancer, Jan 23, 2022.

  1. piscopancer

    piscopancer

    Joined:
    Jul 24, 2021
    Posts:
    15
    I left using PlayersPrefs recently and came for JSON save files. The problem is that I store flexible settings of my game in ScriptableObjects and when I try to save their data in JSON and load back saved data - they corrupt InstanceIDs to assets. That is why you can see in the picture that AudioClips are replaced with Sprites... (what the hellllll).



    My discord – hotdogson#3193

    upload_2022-1-23_10-33-10.png upload_2022-1-23_10-34-10.png upload_2022-1-23_10-40-56.png
     
  2. piscopancer

    piscopancer

    Joined:
    Jul 24, 2021
    Posts:
    15
    This is corruption happening to JSON tho

     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,612
    Just so you know, instanceID's are only valid for that particular session of the Unity Editor or a build. Once they're closed those instanceID's become invalid and won't work any longer. This would be why you might be getting weird references loaded.

    Long story short, you kinda can't save asset references in such a manner or at all despite the efforts I've seen people try.

    You need to break down your data into simple data types (float, strings, etc) and use that to reconstruct your game state.
     
  4. piscopancer

    piscopancer

    Joined:
    Jul 24, 2021
    Posts:
    15
    Do you mean I had better use BinaryFormatter and avoid JSON?
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,612
    I mean nothing of the sort, you just can't serialise references. Binary formatter is unsafe to use anyway.