Search Unity

Playerprefs saving coins

Discussion in 'Scripting' started by sebwin555, Sep 15, 2018.

  1. sebwin555

    sebwin555

    Joined:
    Sep 9, 2018
    Posts:
    22
    Hey guys!

    I have two scenes the "game" scene and the "menu" scene. In the game scene I have the coins score which increases when you collect a coin. Then I used Playerprefs in order to save the collected coins when I die.

    Now when I'm in the menu scene I want my total coin a,mount displayed, but when I use playerprefs in order to get the coins from the last game it updates every time so it only shows the coins I collected during the last game!

    How can I fix this? Thankful for every response!

    this is the game scene method to save the coin score
    Code (CSharp):
    1. public void OnDeath()
    2.     {
    3.  
    4.         deadCointext.text = coinScore.ToString() + "x";
    5.         PlayerPrefs.SetInt ("coins", coinScore);
    6.  
    7.  
    8.  
    9.     }
    this is the menu scene weher I want to have all the coins ever collected
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class mainmenu : MonoBehaviour {
    8.  
    9.     public Text highscoreText;
    10.     public int currencyInRun;
    11.     public Text coinsScore;
    12.  
    13.  
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         highscoreText.text = "HIGHSCORE:" +  ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
    18.  
    19.         currencyInRun = PlayerPrefs.GetInt ("coins");
    20.  
    21.  
    22.         coinsScore.text = "" + currencyInRun;
    23.  
    24.     }
    25. }
     
  2. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    You are setting the coins to be equal to the current game's coins. Obviously you will get the last game coins. What you wanna do is ADD the current game coins to the existing amount of coins.
    Something like :
    Code (CSharp):
    1. coins = PlayerPrefs.GetInt("Coins", coins); //The old amount of coins
    2. PlayerPrefs.SetInt("Coins", coins + coinsScore); //The new amount of coins, aka the old amount of coins + the current game amount of coins.
    3. PlayerPrefs.Save();
     
  3. sebwin555

    sebwin555

    Joined:
    Sep 9, 2018
    Posts:
    22
    but where should I put this, in which script? and I don't think that yours is working, because it saves the coins and in the next one it gets the saved coins plus the new one, but the saved coins and the new ones are the same in this case. Or am I wrong?
     
  4. sebwin555

    sebwin555

    Joined:
    Sep 9, 2018
    Posts:
    22
    Guys anyone has an idea? Pleaseeee
     
  5. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    My code is not wrong but I'll try to make it even simpler...

    Code (CSharp):
    1. int coins;
    2. int thisGameCoins;
    3.  
    4. void Start()
    5. {
    6. coins = PlayerPrefs.GetInt("Coins", 0); //Here we are getting the amount of coins. By default, the first time you play, it will be 0.
    7. }
    8. public void IncreaseCoin()
    9. {
    10. thisGameCoins += 1; //Whatever you do to get a coin.
    11. }
    12. public void GameOver()
    13. {
    14. PlayerPrefs.SetInt("Coins", coins + thisGameCoins); //Here we are setting (saving) the coins to be sum of the old coins + the new coins!
    15. PlayerPrefs.Save();
    16. }
    17.  
     
    sebwin555 likes this.
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,442
    You have the right idea, but you could move line 6 to be just before line 14, and then you don't need a member variable to hold the old coins all the time.
     
  7. DoublePixelStudio

    DoublePixelStudio

    Joined:
    Jan 30, 2017
    Posts:
    69
    I wouldn't use player pref at all for coins. That's sloppy and easy to cheat. I would suggest serialization
     
  8. DoublePixelStudio

    DoublePixelStudio

    Joined:
    Jan 30, 2017
    Posts:
    69
    I'll post my scripts tomorrow for you. Very easy to use.
     
    sebwin555 likes this.
  9. sebwin555

    sebwin555

    Joined:
    Sep 9, 2018
    Posts:
    22
    Yea I read that you can just overwrite the score when using playerprefs, but I'm a noob so I don't really know how to use something else than playerprefs! It would be awsome if you could post it!:)
     
  10. DoublePixelStudio

    DoublePixelStudio

    Joined:
    Jan 30, 2017
    Posts:
    69
    Ok so the first on is your Game script. This holds all your variables like ints, floats, Etc.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collection.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6.  
    7. public class Game
    8. {
    9.  
    10. public static Game current = new Game();
    11. public int _gold;
    12.  
    13. }
    The second one does the serialization. Its called SaveLoad.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Runtime.Serialization.Formatters.Binary;
    3. using System.IO;
    4.  
    5. public static class SaveLoad
    6. {
    7.  
    8.     public static void SaveGame()
    9.     {
    10.         BinaryFormatter bf = new BinaryFormatter();
    11.         if (File.Exists(Application.persistentDataPath + "/SavedGame.dat"))
    12.         {
    13.  
    14.             FileStream file = File.Open(Application.persistentDataPath + "/SavedGame.dat", FileMode.Open);
    15.             Game game = new Game();
    16.          
    17.             game.Gold = Game.current.Gold;// Add your items from Game like this. Remember to do game.Gold for this section.
    18.  
    19.             bf.Serialize(file, Game.current);
    20.             file.Close();
    21.         }
    22.         else
    23.         {
    24.  
    25.             FileStream file = File.Create(Application.persistentDataPath + "/SavedGame.dat");
    26.             Game game = new Game();
    27.          
    28.             game.Gold = Game.current.Gold; // Add your items from Game like this. Remember to do game.Gold for this section.
    29.  
    30.             bf.Serialize(file, Game.current);
    31.         }
    32.  
    33.  
    34.     }
    35.  
    36.     public static void LoadGame()
    37.     {
    38.         if (File.Exists(Application.persistentDataPath + "/SavedGame.dat"))
    39.         {
    40.             BinaryFormatter bf = new BinaryFormatter();
    41.             FileStream file = File.Open(Application.persistentDataPath + "/SavedGame.dat", FileMode.Open);
    42.             Game game = (Game)bf.Deserialize(file);
    43.             file.Close();
    44.  
    45.             Game.current.Gold = game.Gold;// Add your items from Game like this. Remember to do Game.current.Gold for this section.
    46.          
    47.  
    48.  
    49.             Debug.Log("Loaded The File");
    50.         }
    51.         else
    52.         {
    53.             Debug.Log("File Does Not Exist");
    54.         }
    55.     }
    56. }
    57.  
    So when ever you wanna add to your gold or score you would just add to the gold int.

    Code (CSharp):
    1. Game.current.Gold ++;
    And then you wanna make sure that you save this when ever your wanting to save gold amount. That way when the user re opens your game they have their gold saved. You can do this for high score saving also. But if your using this for score solely then you want to just say in start function

    Code (CSharp):
    1. Game.current.Gold =0;
    And then you can pull that to use in your score UI.

    To save it
    Code (CSharp):
    1. SaveLoad.SaveGame();
    And then load the file at the beginning of the scene where your score is held

    Code (CSharp):
    1. SaveLoad.LoadGame();
     
    sebwin555 likes this.
  11. sebwin555

    sebwin555

    Joined:
    Sep 9, 2018
    Posts:
    22
    Wow thank you so much! I'll try it!
     
  12. DoublePixelStudio

    DoublePixelStudio

    Joined:
    Jan 30, 2017
    Posts:
    69
    Np. Let me know if you have any questions about it.
     
  13. sebwin555

    sebwin555

    Joined:
    Sep 9, 2018
    Posts:
    22
    Okay I tried to implement it into my game, but I'm not quite sure where to put all that...
     
  14. sebwin555

    sebwin555

    Joined:
    Sep 9, 2018
    Posts:
    22
    it worked!
     
    meetkachhadiya821 likes this.
  15. vatsasree150

    vatsasree150

    Joined:
    Feb 21, 2021
    Posts:
    1
    Hey mate!!! I am new to Unity. What did you do to make it work?
     
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Definitely start with some tutorials.

    Screen Shot 2021-05-18 at 8.50.46 AM.png

    When you get stuck, PLEASE start your own post according to forum rules.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/