• 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
Question by Abacab · Dec 22, 2014 at 01:57 PM · c#playerprefscurrency

Playerprefs and collecting currency

Hello!

I'm having a problem with playerprefs.

I'm trying to add an in-game currency, and while I am able to "collect" the currency, I'm not able to update the amount of currency collected in real-time. It now only adds +1 to my currency when I reset the level (if I have collected >1 currency).

Here is my script. Thanks in advance!

 public class CoinTrigger : MonoBehaviour
 {
         public GameObject Coin;
         public int coins = 0;
         public Text coinAmount;
 
         // Use this for initialization
         void Start ()
         {
                 coins = PlayerPrefs.GetInt ("Coins");
                 
         }
     
         // Update is called once per frame
         void Update ()
         {
                 transform.Rotate (0, 0, 50 * Time.deltaTime);
         }
 
         void OnTriggerEnter ()
         {
                 if (Coin.gameObject.tag == "Coin") {
                         PlayerPrefs.SetInt ("Coins", coins + 1);
                         Destroy (Coin.gameObject);
                         coins = PlayerPrefs.GetInt ("Coins");
                 }
         }
 
         void OnGUI ()
         {
                 coinAmount.text = coins.ToString ();
         }
             
 }
Comment

People who like this

0 Show 11
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
avatar image Abacab · Dec 22, 2014 at 09:58 PM 0
Share

To which object should I add the script? I now have it on every single coin, which seems stupid but I can't find a better solution.

And it is still not working.

 public class CoinTrigger : MonoBehaviour
 {
         public GameObject Coin;
         public int coins = 0;
         public int coinsCollectedThisLevel = 0;
         public Text coinAmount;
         public Text coinsCollected;
 
         // Use this for initialization
         void Start ()
         {
                 coins = PlayerPrefs.GetInt ("Coins");
                 
         }
     
         // Update is called once per frame
         void Update ()
         {
                 transform.Rotate (0, 0, 50 * Time.deltaTime);
         }
 
         void OnTriggerEnter ()
         {
                 if (Coin.gameObject.tag == "Coin") {
                         coinsCollectedThisLevel++;
                         Destroy (Coin.gameObject);
 
 
                         //coins = PlayerPrefs.GetInt ("Coins");
                         //coins = coins + coinsCollectedThisLevel;
                 }
         }
 
         void OnGUI ()
         {
                 coinAmount.text = coins.ToString ();
                 coinsCollected.text = coinsCollectedThisLevel.ToString () + "/9";
         }
             
 }

avatar image RedHedZed · Dec 22, 2014 at 10:08 PM 0
Share

In your original script up there you never increase the number of coins. You only set a playerpref to be equal to coins+1. This does not actually change the value of coins though. Therefore it will always equal the start value of coins plus 1.

In the second version of the script that you posted in that comment there, you increase the value of a coin counting variable but never tell playerprefs to remember it.

So what you have to do is increase the value of coins and then tell playerprefs to remember this new value.

avatar image RedHedZed · Dec 22, 2014 at 10:10 PM 0
Share

Hang on, I see how you were trying to increase the value of coins. By settings the playerpref and then getting the new value.

Sorry, I didn't see that on my first go. The reason it wasn't working is because you were destroying the game object before getting the new coin value. Once you destroy the gameobject, you destroy all of its components as well, which I imagine includes this script. This means that any code that comes after the destroy will never be executed.

avatar image Abacab · Dec 22, 2014 at 10:18 PM 0
Share

Thanks for the replies. I still have the issue that the number of coins only updates after I reset the level, and it only adds 1 coin even if I collect several.

Also, as I said I have added the script to every individual coin (object). Is there a better way?

 void OnTriggerEnter ()
         {
                 if (Coin.gameObject.tag == "Coin") {
                         coins += 1;
                         coinsCollectedThisLevel += 1;
                         PlayerPrefs.SetInt ("Coins", coins);
                         coins = PlayerPrefs.GetInt ("Coins");
                         Destroy (Coin.gameObject);
                 }
         }
avatar image RedHedZed · Dec 22, 2014 at 10:45 PM 0
Share

You shouldn't need to say:

 coins = PlayerPrefs.GetInt("Coins");

You already increase the value of coins several lines earlier, so this is redundant.

ExtremePowers is right though, where are you saving? At the end of your level, before leaving it or resetting it or whatever you do, try saving.

 PlayerPrefs.Save()


Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by ExtremePowers · Dec 22, 2014 at 10:06 PM

Where are you saving?

 PlayerPrefs.Save();

and you need to add 1 to the current value¨

 coins += 1;
 PlayerPrefs.SetInt ("Coins", coins);

or

 coins = PlayerPrefs.GetInt ("Coins");
 PlayerPrefs.SetInt("Coins", coins + 1);
Comment

People who like this

0 Show 1 · 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
avatar image Abacab · Dec 22, 2014 at 10:19 PM 0
Share

It saves my playerpref high scores automatically. Is there really a need to save?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Currency System Not Saving!? 1 Answer

Distribute terrain in zones 3 Answers

Local Leaderboard with unique names 1 Answer

Unity2d - Player wont teleport to the positon after saving x/y positions with player prefs 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