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 /
  • Help Room /
avatar image
0
Question by Tageos · Jan 05, 2016 at 09:02 PM · c#playerprefssave data

Save coins/currency?

Hello!

Using player prefs my game stores the currency the player has aquired during his run, when its game over i store the currency:

 if (game.gameOver == true && tempsReset == false){
     Debug.Log("Iron collected: " +ironTemp);
     resetTemps = true;
     PlayerPrefs.SetInt ("currencyPref", currency);
 }

When i then exit play mode and enter it again i can see the currency from the previous game session in my currency manager:

 public int currency;
 
 void Start () {
    currency = PlayerPrefs.GetInt("currencyPref");
 }

Until this point everything works fine.

But after the second gamesession the first currency gets overwritten...

If i exit playmode, re-enters and play unitil its game over i want to see the currency gained from ALL the sessions prior to this one. Not only the previous one. How would i achive this?

Comment
Add comment · Show 1
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 ShadyProductions · Jan 05, 2016 at 10:28 PM 0
Share

Why do you want to see all the currencies gained, why not make a new int and put all the totals on there, so you have an out of game total earned, and your gameover screen gains of the current game? But why show all, say if I play 100 games, where do I keep all of them?:P

2 Replies

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

Answer by hulahoolgames · Jan 05, 2016 at 10:35 PM

When you store the currency after a run, you should add the currency earned in this run to the current balance you have read in the Currency Manager.

 public int currentBalance;
 public int currencyInRun = 0;
 
 void Start() {
      // read the current balance from Player Prefs
      currentBalance = PlayerPrefs.GetInt("currencyPref");
 }
 
 void DuringRun() {
      // during run keep incrementing when player earns coins
      currencyInRun++;
 }
 
 void OnGameOver() {
     // Do what you want
     // add currency earned to the total balance
     currentBalance += currencyInRun; 
     PlayerPrefs.SetInt("currencyPref", currentBalance);
     currencyInRun = 0; // reset this for the next round
 }

Here is a basic idea. You can maintain 2 variables. One that holds the current balance in your bank and the other one holds the currency you have earned in this run. After the game completes, you add the currency in this run to the bank balance and save it in player prefs. Make sure you reset the variable for in round currency before a next round. Hope this helps.

Comment
Add comment · Show 2 · 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 Tageos · Jan 05, 2016 at 11:34 PM 0
Share

Thank you so much! This really helped :)

avatar image hulahoolgames Tageos · Jan 06, 2016 at 12:09 AM 1
Share

No problem :) glad it did!

avatar image
0

Answer by LZYGYZ · May 28, 2017 at 04:24 PM

Thanx man it work for me

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

55 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

Related Questions

How to reference data between scenes 1 Answer

A Little Help With Player Prefs 0 Answers

How can I save this objects ? 0 Answers

Saving last runs score, only adds the previous score to it. 0 Answers

Application.persistentDataPath 0 Answers


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