Y
Y
Yura Mailler2020-12-28 20:20:13
Unity
Yura Mailler, 2020-12-28 20:20:13

Why json is not saved?

Hello! For some reason, json does not want to save data and resets the data when the game ends.
the script is responsible for issuing a random hero

{
    private string jsonData = "Random Text";
    private string path;

    public int[] Mass = { 30, 25, 15, 10, 5 };
    public bool[] Persi;

    public bool ItTrue = false;

    public int q = 0;
    public int i = 0;
    public int Rand;
    public int Num;
    public int total;
    public int Money;

    public Transform pos ;

    public Sprite[] Charectors;

    private Save sv = new Save();
    
    

    void Awake()
    {
        Money = PlayerPrefs.GetInt("Money_S", 0);
    }
    void Start()
    {
        
        PlayerPrefs.SetInt("NumTrue", i);
        PlayerPrefs.SetInt("Money_S", Money);
        Money = PlayerPrefs.GetInt("Money_S", 0);
        Money += 50;


#if UNITY_ANDROID && !UNITY_EDITOR
        path = Path.Combine(Application.persistentDataPath, "Save.json");
#else
        path = Path.Combine(Application.dataPath, "Save.json"); // путь файла
#endif
        
        foreach (var item in Mass)
        {
            total += item;
        }

        Num = UnityEngine.Random.Range(0, total);

        for(; i < Mass.Length; i++)
        {
            if(Num <= Mass[i])
            {
                this.GetComponent<SpriteRenderer>().sprite = Charectors[i];
                Persi[i] = true;
                return;
            }
            else
            {
                Num -= Mass[i];
            }
        }

        



        if (File.Exists(path))//если файл есть
        {
            sv = JsonUtility.FromJson<Save>(File.ReadAllText(path)); // присваеваем 
            
            Debug.Log("присвоили");
        }
        else
        {

        }
        ItNUM();
        

    }

    

#if UNITY_ANDROID && !UNITY_EDITOR
        private void OnApplicationPause(bool pause){
            if(paise) File.WriteAllText(path, JsonUtility.ToJson(sv));

        }
#endif


    private void OnApplicationQuit()
    {
        File.WriteAllText(path, JsonUtility.ToJson(sv));
        Debug.Log("сохранили");
        PlayerPrefs.SetInt("Money_S", Money);
    }

    void Update()
    {
        sv.Persi = Persi;
        
        File.WriteAllText(path, JsonUtility.ToJson(sv));
        PlayerPrefs.SetInt("NumTrue", i);
    }
    
    
        
    
    
    
    
    

    [Serializable]
    public class Save
    {
        public bool[] Search;
        public bool[] Persi;
        
    }


    public void ItNUM()
    {
        if(Num != i)
        {
            sv.Search[i] = true;
        }
    }
}

if something is not clear in the code, write

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-12-28
@freeExec

This is called debugging your buggy code. Learn to do it. With questions about each code that does not work, you will not go far on the forums.
If you don’t know how to work with a debugger, put logs around your code, where and what you have at what moment.
I’m already silent, about the fact that the code for androyd will not even be assembled:

#if UNITY_ANDROID && !UNITY_EDITOR
        private void OnApplicationPause(bool pause){
            if(paise) File.WriteAllText(path, JsonUtility.ToJson(sv));

        }
#endif

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question