Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mortyc · Mar 11, 2018 at 12:15 AM · playerprefssettingsadding

How to store and add gold for the player ?

Hi i am trying to store and add the gold for the player every time he plays the game. Now i used this code but it doesnt seems to work as intended. To make this clear I want to store the gold earned in the level and then add it up with the gold earned before . public Text goldtxt; public Text score;

 public class GoldAmount : MonoBehaviour
 {
 
     public Text goldtxt;
     public Text score;
 
     // Use this for initialization
     void Start()
     {
 
 
         int cur_gold = PlayerPrefs.GetInt("Gold");
         int gold = PlayerPrefs.GetInt("CurrentGold");
         PlayerPrefs.SetInt("CurrentGold", cur_gold);
         goldtxt.text = "Gold " + (cur_gold + gold);
 
 
 
         int CurScore = PlayerPrefs.GetInt("Score");
         score.text = "Last Score: " + CurScore;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Xarbrough · Mar 11, 2018 at 12:48 AM

You can try something like this:

 public class GoldAmount : MonoBehaviour
 {
     public Text text;
 
     private int gold;
     private bool wasInitialized;
 
     private const string key = "Gold";
 
     private void OnEnable()
     {
         // When the game starts, load the saved gold amount.
         // Make sure, nothing else modifies gold before this was called.
         gold = PlayerPrefs.GetInt(key, 0);
         wasInitialized = true;
     }
 
     private void OnDisable()
     {
         // When the game ends, save the gold amount.
         PlayerPrefs.SetInt(key, gold);
         // This could also go in OnDestroy or OnApplicationQuit
         // Or somehwere else to manually save.
         wasInitialized = false;
     }
 
     private void OnGoldChanged()
     {
         text.text = "Gold: " + gold.ToString();
     }
 
     public int Gold
     {
         get { return gold; }
         set
         {
             if (wasInitialized == false)
             {
                 Debug.LogError("Cannot set gold before initialization.");
                 return;
             }
 
             if (gold != value)
             {
                 gold = value;
                 OnGoldChanged();
             }
         }
     }
 }

You should clearly define when gold is loaded, modified and saved. I would simply load the last saved amount in OnEnable and make sure, that we don't modify the gold before this point. After initialization, we can simply change the variable and once we are ready to save (e.g. user clicks a button or the app quits), we save (in e.g. OnDisable, OnDestroy, OnApplicationQuit, etc).

However, PlayerPrefs is not meant to store game save data such as gold and score values. It is meant for player preference (e.g. audio volume, difficulty-setting or other lightweight and non-security critical values).

There are other ways to save data, as shown in this Unity tutorial: Persistance - Saving and Loading Data

The process of saving data is also called "serialization". Commonly used are binary serialization (via the BinaryFormatter class from the .Net framework), JSON (e.g. Unity's JsonUtility class or plugins like Newtonsofts Json.NET) and XML.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ingame Quality Settings not recognized 1 Answer

Unity 5 not using my Resolution Settings 2 Answers

Saving Post Processing as a graphic option 1 Answer

In Game Graphics/Resolution Options 2 Answers

options menu UI doesnt save when switching scenes 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges