0

I have a problem, json quietly saves the file and when you need to download it, nothing comes out. When you start the game, all parameters become false and I can't understand why . I will be glad if someone can help. also, if something is not clear in the code, write I will explain what the code is responsible for . Here is the code where to save the file

`

    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"); //the path of the file
        #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))//if file is there
        {
            sv = JsonUtility.FromJson<Save>(File.ReadAllText(path)); // assign

            Debug.Log("assign");
        }
        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("save");
        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;
        }
    }

`

end Load

    private Save sv = new Save();

    public bool[] Persi;

    private string path;
    private string path2;

    
    public GameObject[] Players;
    public bool[] ItSkin;
    public int Num;
    public Vector2 PosActive;
    private int NumPlayer;
    public int q;
    
    public Vector2 PosNotActive;

    public int NumberPlayer = 0;
    public GameObject NotSkin;
    public int bla = 0;
    void Awake()
    {
        q = PlayerPrefs.GetInt("NumTrue" , 0);
        //Persi[q] = true;  
        
    }
    void Start()
    {

        

#if UNITY_ANDROID && !UNITY_EDITOR
        path = Path.Combine(Application.persistentDataPath, "Save.json");
        
#else
        path = Path.Combine(Application.dataPath, "Save.json"); // путь файла

#endif
        
        if (File.Exists(path))//если файл есть
        {
            sv = JsonUtility.FromJson<Save>(File.ReadAllText(path)); // присваеваем 

            Debug.Log("присвоили");
        }
        else
        {

        }
        for(int e = 0; e < Persi.Length; e++){
            Debug.Log(Persi[e]);
        }


    }
    public void PersMas(){
        
    }
#if UNITY_ANDROID && !UNITY_EDITOR
private void OnApplicationPause(bool pause){
            PlayerPrefs.SetInt("PlayerNum", NumberPlayer);
            

        }
#endif

    private void OnApplicationQuit()
    {
        
        PlayerPrefs.SetInt("PlayerNum", NumberPlayer); 
       
    }
    
    void Update()
    {
        sv = JsonUtility.FromJson<Save>(File.ReadAllText(path)); // присваеваем 
        sv.Persi = Persi;
        //Persi[q] = true;
        Num = PlayerPrefs.GetInt("Num" , 0);
        PlayerPrefs.SetInt("PlayerNum", NumberPlayer);

        
        if(bla == 16 && NumPlayer == 16){
            bla = 0;
            NumberPlayer = 0;
        }
        
    }

    public void Next(){
        switch(bla){
            case 0:
                bla++;
                NumPlayer = 1;
                if(Persi[0] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 1;
                    NotSkin.SetActive(false);
                }
                
                Players[0].SetActive(false);
                Players[1].SetActive(true);
                break;
            case 1:
                bla++;
                NumPlayer = 2;
                if(Persi[1] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 2;
                    NotSkin.SetActive(false);
                }
                Players[1].SetActive(false);
                Players[2].SetActive(true);
                break;
            case 2:
                bla++;
                NumPlayer = 3;
                if(Persi[2] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 3;
                    NotSkin.SetActive(false);
                }
                Players[2].SetActive(false);
                Players[3].SetActive(true);
                break;
            case 3:
                bla++;
                NumPlayer = 4;
                if(Persi[3] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 4;
                    NotSkin.SetActive(false);
                }
                Players[3].SetActive(false);
                Players[4].SetActive(true);
                break;
            case 4:
                bla++;
                NumPlayer = 5;
                if(Persi[4] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 5;
                    NotSkin.SetActive(false);
                }
                Players[4].SetActive(false);
                Players[5].SetActive(true);
                break;
            case 5:
                bla++;
                NumPlayer = 6;
                if(Persi[5] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 6;
                    NotSkin.SetActive(false);
                }
                Players[5].SetActive(false);
                Players[6].SetActive(true);
                break;
            case 6:
                bla++;
                NumPlayer = 7;
                if(Persi[6] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 7;
                    NotSkin.SetActive(false);
                }
                Players[6].SetActive(false);
                Players[7].SetActive(true);
                break;
            case 7:
                bla++;
                NumPlayer = 8;
                if(Persi[7] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 8;
                    NotSkin.SetActive(false);
                }
                Players[7].SetActive(false);
                Players[8].SetActive(true);
                break;
            case 8:
                bla++;
                NumPlayer = 9;
                if(Persi[8] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 9;
                    NotSkin.SetActive(false);
                }
                Players[8].SetActive(false);
                Players[9].SetActive(true);
                break;
            case 9:
                bla++;
                NumPlayer = 10;
                if(Persi[9] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 10;
                    NotSkin.SetActive(false);
                }
                Players[9].SetActive(false);
                Players[10].SetActive(true);
                break;
            case 10:
                bla++;
                NumPlayer = 11;
                if(Persi[10] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 11;
                    NotSkin.SetActive(false);
                }
                Players[10].SetActive(false);
                Players[11].SetActive(true);
                break;
            case 11:
                bla++;
                NumPlayer = 12;
                if(Persi[11] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 12;
                    NotSkin.SetActive(false);
                }
                Players[11].SetActive(false);
                Players[12].SetActive(true);
                break;
            case 12:
                bla++;
                NumPlayer = 13;
                if(Persi[12] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 13;
                    NotSkin.SetActive(false);
                }
                Players[12].SetActive(false);
                Players[13].SetActive(true);
                break;
            case 13:
                bla++;
                NumPlayer = 14;
                if(Persi[13] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 14;
                    NotSkin.SetActive(false);
                }
                Players[13].SetActive(false);
                Players[14].SetActive(true);
                break;
            case 14:
                bla++;
                NumPlayer = 15;
                if(Persi[14] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 15;
                    NotSkin.SetActive(false);
                }
                Players[14].SetActive(false);
                Players[15].SetActive(true);
                break;
            case 15:
                bla++;
                NumPlayer = 16;
                if(Persi[15] == false){
                    NotSkin.SetActive(true);
                }
                else{
                    NumberPlayer = 0;
                    NotSkin.SetActive(false);
                }
                Players[15].SetActive(false);
                Players[0].SetActive(true);
                break;          
            
            default:
                break;
        }
    }
    
        public void CloseSkin(){
        SceneManager.LoadScene("menu");
    }

    [Serializable]
    public class Save
    {
        public bool[] Search;
        public bool[] Persi;
    }
Yura
  • 3
  • 1

1 Answers1

0

I think your main issue is

sv = JsonUtility.FromJson<Save>(File.ReadAllText(path)); // присваеваем 
sv.Persi = Persi;

so you load your values from file but then immediately overwrite them with the existing Persi array.

Also you seem to load and save every frame so what happens if for some reason you load before you save?

-> You overwrite the currently changed values with the loaded ones -> you never make any progress.


As already mentioned in the comments there are other "problems" here:

  • You load and save the file every frame. That is not only very inefficient but also unnecessary. FileIO is extremely expensive and you should avoid it as long as possible.

    Load once at start and then only save on certain events (like when something actually changes) or certain time intervals

    Also the order is important. Imagine you always load the file before you save => you always keep the old values since the currently changed ones get overwritten by the loading...

  • And why even save a local array with the same bool values? Simply directly make all your changes in sv.Persi and load and save it.

    Do not reassign the array.

    If you need it pulic rather make e.g. a property like

    public bool[] Persi => sv.Persi;
    

In general I would suggest to have a look at How to pass data between Scenes for multiple ways of how to forward these values between multiple scenes also without permanently doing FileIO operations.

derHugo
  • 83,094
  • 9
  • 75
  • 115