Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why is the array being reset to zero

Discussion in 'Scripting' started by az3911777, Jan 8, 2021.

  1. az3911777

    az3911777

    Joined:
    Jan 8, 2021
    Posts:
    8
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5. using System.IO;
    6. public class SaveLoadManager
    7. {
    8.     public static void SavePlayer(Player player)
    9.     {
    10.         BinaryFormatter bf = new BinaryFormatter();
    11.         FileStream stream = new FileStream(Application.persistentDataPath + "/player.sav", FileMode.Create);
    12.         PlayerData data = new PlayerData(player);
    13.         bf.Serialize(stream, data);
    14.         stream.Close();
    15.     }
    16.     public static int[] LoadPlayer()
    17.     {
    18.         if (File.Exists(Application.persistentDataPath + "/player.sav"))
    19.         {
    20.             BinaryFormatter bf = new BinaryFormatter();
    21.             FileStream stream = new FileStream(Application.persistentDataPath + "/player.sav", FileMode.Open);
    22.             PlayerData data = bf.Deserialize(stream) as PlayerData;
    23.             stream.Close();
    24.             return data.stats;
    25.         }
    26.         else
    27.         {
    28.             Debug.LogError("FFFEEELLEE NNNOOOTTT EXISSSTTT");          
    29.             return null;
    30.         }
    31.     }
    32. }
    33. [Serializable]
    34. public class PlayerData
    35. {
    36.     public int[] stats = new int[10];
    37.     public PlayerData(Player player)
    38.     {
    39.         stats[player.myday] = 45;
    40.     }
    41. }
    The second script :

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. public class Player : MonoBehaviour
    4. {
    5.     public int myday;  
    6.     public void Start()
    7.     {
    8.         myday = System.DateTime.Now.Day;
    9.     }
    10.     public void Save()
    11.     {
    12.         SaveLoadManager.SavePlayer(this);
    13.     }
    14.     public void Load()
    15.     {
    16.         int[] loadedStats = SaveLoadManager.LoadPlayer();
    17.         for (int j = 0; j < loadedStats.Length; j++)
    18.         {
    19.             Debug.Log("DAY   " + (j + 1) + " - " + loadedStats[j]);
    20.         }
    21.     }
    22. }

    I create an array and write the number 45 to the cell by day number
    Everything works the array is saved to the file /player. sav

    BUT! When the day is updated, yesterday's cell is again zero . How do I fix this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
  3. az3911777

    az3911777

    Joined:
    Jan 8, 2021
    Posts:
    8
  4. az3911777

    az3911777

    Joined:
    Jan 8, 2021
    Posts:
    8
    Well. But how do I make the number persist? It's real ?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Serialize the data via JSON and it will now be a string.

    Write that string to a file, or store it in PlayerPrefs, or send it to a server... up to you!
     
  6. az3911777

    az3911777

    Joined:
    Jan 8, 2021
    Posts:
    8
    Please look at the code, maybe I did not finish something. I want to beat him :). Please !
     
  7. az3911777

    az3911777

    Joined:
    Jan 8, 2021
    Posts:
    8
    How to overwrite a class instance without creating a new one ?

    12 PlayerData data = new PlayerData(player);
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Depends on the JSON library but with JSON .NET the function is PopulateObject();