Search Unity

Save score in my 2d game

Discussion in '2D' started by zaros_unity, Oct 17, 2019.

  1. zaros_unity

    zaros_unity

    Joined:
    Jul 15, 2018
    Posts:
    11
    Hi!
    I'm trying to save my 'coins' and 'points' and 'waves' in my 2d game I'm working on..
    I've tried dontdestroyonload but didn't work for what i needed so I've looked into saving as playerprefs but im new to all of this..
    I had 50levels and 50 difference scenes but decided to just have all levels on 1 scene and just use timers to display 'Next Level' text etc but I need a way to save my coins, castle level, points, and upgrades purchased..
    This is the script which i believe must be edited to allow saving..
    I'm new to this so please dumb down answers , i just want my game to save and load how i left it when i exit, thank you! :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class ResourcesCastle : MonoBehaviour {
    5.  
    6.     public int Coins;
    7.     public int Experience;
    8.     public int LevelCastle = 1;
    9.     public int Wave = 1;
    10.  
    11.     public float Timers = 120f;
    12.  
    13.     public GameObject Cl1;
    14.     public GameObject Cl2;
    15.     public GameObject Cl3;
    16.  
    17.     public Text CoinsText;
    18.     public Text ExperienceText;
    19.     public Text LevelCastleText;
    20.     public Text WaveText;
    21.     public Text Timer;
    22.  
    23.     private float times;
    24.  
    25.     private bool Wave2 = false;
    26.     private bool Wave3 = false;
    27.     private bool Wave4 = false;
    28.     private bool Wave5 = false;
    29.  
    30.     void Start()
    31.     {
    32.         times = Timers;
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.  
    38.         WaveUp();
    39.         Texts();
    40.         Waves();
    41.         LvlCastle();
    42.  
    43.     }
    44.  
    45.     void WaveUp()
    46.     {
    47.      
    48.         if(Wave5 == true)
    49.         {
    50.             Wave = 5;
    51.         }
    52.  
    53.         if(Wave == 2 && Wave2 == false)
    54.         {
    55.             gameObject.GetComponent<EnemySpawn>().TimerMax -= 2;
    56.             Wave2 = true;
    57.         }
    58.  
    59.         if(Wave == 3 && Wave3 == false)
    60.         {
    61.             gameObject.GetComponent<EnemySpawn>().TimerMax -= 3;
    62.             Wave3 = true;
    63.         }
    64.         if(Wave == 4 && Wave4 == false)
    65.         {
    66.             gameObject.GetComponent<EnemySpawn>().TimerMin -= 1;
    67.             gameObject.GetComponent<EnemySpawn>().TimerMax -= 4;
    68.             Wave4 = true;
    69.         }
    70.         if (Wave == 5 && Wave5 == false)
    71.         {
    72.             gameObject.GetComponent<EnemySpawn>().TimerMin -= 1;
    73.             gameObject.GetComponent<EnemySpawn>().TimerMax -= 5;
    74.             Wave5 = true;
    75.         }
    76.     }
    77.  
    78.     void Texts()
    79.     {
    80.         CoinsText.text = Coins.ToString();
    81.         ExperienceText.text = Experience.ToString();
    82.         LevelCastleText.text = LevelCastle.ToString();
    83.         WaveText.text = "Wave:" + Wave.ToString();
    84.     }
    85.  
    86.     void Waves()
    87.     {
    88.         times -= Time.deltaTime;
    89.         if(times <= 0)
    90.         {
    91.             Wave += 1;
    92.             times = Timers;
    93.         }
    94.     }
    95.  
    96.     void LvlCastle()
    97.     {
    98.         if (Cl1.activeInHierarchy == true)
    99.         {
    100.             LevelCastle = 1;
    101.         }
    102.         if (Cl2.activeInHierarchy == true)
    103.         {
    104.             LevelCastle = 2;
    105.         }
    106.         if (Cl3.activeInHierarchy == true)
    107.         {
    108.             LevelCastle = 3;
    109.         }
    110.     }
    111. }
    112.  
     
    Last edited: Oct 17, 2019
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    PlayerPrefs are very easy to use and reading about them and seeing examples should be all you need:
    https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

    You can do 5 things with PlayerPrefs:
    1. Store a value (int, float, string)
    2. Save PlayerPrefs current values to the local storage
    3. Check if PlayerPrefs contains a value previously stored
    4. Retrieve a value (int, float, string)
    5. Delete values from the storage
    Here's an example of each thing:
    Code (CSharp):
    1. private int coins;
    2. private string coins_key = "coins";
    3.  
    4. public void Save()
    5. {
    6.     PlayerPrefs.SetInt(coins_key, coins); // 1
    7.     PlayerPrefs.Save(); // 2
    8. }
    9.  
    10. public void Load()
    11. {
    12.     if (PlayerPrefs.HasKey(coins_key)) // 3
    13.     {
    14.         coins = PlayerPrefs.GetInt(coins_key); // 4
    15.     }
    16. }
    17.  
    18. public void Clear()
    19. {
    20.     PlayerPrefs.DeleteKey(coins_key); // 5
    21.     PlayerPrefs.Save(); // 2
    22. }
    Unity will automatically call "PlayerPrefs.Save" when the application quits normally, but if it crashes or something, you will lose data. So it's best to save at regular intervals in code.
     
    Last edited: Oct 17, 2019
    vakabaka and zaros_unity like this.
  3. zaros_unity

    zaros_unity

    Joined:
    Jul 15, 2018
    Posts:
    11
    Thank you for the reply!
    Ive taken a look at the playerprefs help section multiple times, im still new to coding and all that and I've no idea where to even put the code inside the script (im sorry).

    Also, like you said if it crashes unexpectedly then it wont save which would be a huge problem if i have a bug in my game or simply if the players phone dies, so I'd need to code a script and attach to a save button for example that will save via the scripts code etc?

    Sorry for the stupid questions but I've been working on this game for a while and I've just suddenly hit a brick wall haha!
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Choose a time you want to save the game. It could be every time you change your coins and other values, it could be at the end of a level, or only when the player presses a button. Then at that time, call your "Save" function, which will store all the values into PlayerPrefs and save them. You can add to the function so that it saves coins as well as anything else you want.

    When the game starts up, use a Start or Awake function to call "Load", which will assign your "coins" variable and others with the stored values.

    That's pretty much it. Load on startup, save when things change.
     
    zaros_unity likes this.
  5. zaros_unity

    zaros_unity

    Joined:
    Jul 15, 2018
    Posts:
    11
    Thanks again for the reply and trying to help! I hope someone sees this thread and also gets help, i have messaged you as im a little confused though :s