1

Hi I am using a scriptibleobject in unity that holds data for the store in my game it has been working fine for awhile now, but when I opened unity today after updating unity hub I got this error in my code

ArgumentException: JSON parse error: The document is empty. UnityEngine.JsonUtility.FromJsonOverwrite (System.String json, System.Object objectToOverwrite) (at <1386288601af43018501cce2912f52f4>:0)

it trowed the error anywhere I referenced the LoadDataFromFile function in my ScriptableObjec objects code.The error was on line 50 of my code. thank you for your time

here is the ScriptableObjects code

using System.Collections.Concurrent;
using System.ComponentModel.Design;
using UnityEngine;
using System.IO;
using System;
[CreateAssetMenu(fileName = "data",menuName = "sss",order = 1)]

public class sss : ScriptableObject
{
public float coins = 0f;
public float speed = 10f;
public float jump = 25f;
public bool buyspeed = false;
public bool buyjump = false;
public bool shotgununlock = false;
public bool set = true;
public int face = 1;
public int gun = 1;
public bool sniperunlock = false;


private const string FILENAME = "sss.dat";

public void SaveToFile()
{
    var filePath = Path.Combine(Application.persistentDataPath, FILENAME);

    if (!File.Exists(filePath))
    {
        File.Create(filePath);
    }

    var json = JsonUtility.ToJson(this);
    File.WriteAllText(filePath, json);
}


public void LoadDataFromFile()
{
    var filePath = Path.Combine(Application.persistentDataPath, FILENAME);

    if (!File.Exists(filePath))
    {
        //Debug.LogWarning($"File /"{ filePath}/ " not found!, this);
        return;
    }

    var json = File.ReadAllText(filePath);
    JsonUtility.FromJsonOverwrite(json, this); //<-----this line of code
}
}
Powplowdevs
  • 55
  • 1
  • 2
  • 8
  • `The document is empty.` sounds quite self-explanatory .. check the file path (`Debug.Log(filePath);`) and see if the file content is actually what you expect it to be ... – derHugo Jun 14 '20 at 12:44

1 Answers1

0

I ended up fixing it by just renaming JSON file and and replacing this line of code private const string FILENAME = "sss.dat"; with this private const string FILENAME = "NEW FILE NAME";

Powplowdevs
  • 55
  • 1
  • 2
  • 8