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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Save Load System

Discussion in 'Scripting' started by caketaker, Apr 22, 2020.

  1. caketaker

    caketaker

    Joined:
    Apr 12, 2020
    Posts:
    2
    Hi all,

    I need some help I am very new to C# and unity and have only started using both in the last 3 weeks.

    I have developed a idle clicker game in that time teaching myself along the way but now i need it to actually save, load and recorded offline progress. I have read many different guides and even tried purchasing the easy save asset from the asset store (https://assetstore.unity.com/packag...nt/easy-save-the-complete-save-load-asset-768) but i am still no further forward on how to get that asset to work or how to set up a save load system.

    Please can anyone help.

    this is some of the values i need to save

    Code (CSharp):
    1.    
    2.     public static float energy;
    3.     public static float storage;
    4.     public static float worker;
    5.     public static float warehouse;
    6.     public static float house;
    7.     public static float powerPlant;
    8.  
    9.     public static int energyPowerUpgradeValue = 10; //test value
    10.     public static int storagePowerUpgradeValue = 10; //test value
    11.     public static int workerPowerUpgradeValue = 10; //test value
    12.     public static int warehousePowerUpgradeValue = 10; //test value
    13.     public static int housePowerUpgradeValue = 10; //test value
    14.     public static int powerPlantPowerUpgradeValue = 10;//test value
    15.  
    16.     public static int energyPowerUpgradeCost = 100;
    17.     public static int storagePowerUpgradeCost = 100;
    18.     public static int workerPowerUpgradeCost = 100;
    19.     public static int warehousePowerUpgradeCost = 100;
    20.     public static int housePowerUpgradeCost = 100;
    21.     public static int powerPlantPowerUpgradeCost = 100;
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    1. Get a GameManager object that sticks around your game to keep track of these values.
    2. Wrap these values in a [Serializable] data class (that your GameManager keeps track of) instead of them being static variables.
    3. Give your GameManager Save() and Load() functions, and you can serialize/deserialize your data class with Unity's build in JsonUtility: https://docs.unity3d.com/ScriptReference/JsonUtility.html. Have GameManager save the json to a file or read it back from a file.
     
    caketaker likes this.
  3. caketaker

    caketaker

    Joined:
    Apr 12, 2020
    Posts:
    2
    I will take a look at that thanks