Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 vincentamato · Dec 23, 2014 at 06:46 PM · gamevariablescore systemmoneysystem

Combined Total of Different Variable Values

Hi. In my game for every second you survive you get a point. This is your score. You receive a score every time you play. You also have a high score. Now what I want to do is for every point you get you also get a coin. My problem is when I display your money amount on the menu it is just what you score last round. How can I make it so that your money amount is a collective total value of all your scores? Here is the code I am using to display your money amount which is the same as your score:

Saving the money amount:

 PlayerPrefs.SetInt ("moneyAmount", score); 

Displaying it:

     private int moneyAmount; 
     public Text moneyamountLabel; 
 
 
     // Use this for initialization
     void Start () {
 
         moneyAmount = PlayerPrefs.GetInt ("moneyAmount");
         moneyamountLabel.text = moneyAmount.ToString(); 
     
     }
     

Any ideas? Thank you! :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Victory Dan Greene · Dec 24, 2014 at 07:10 AM

It seems like when you save moneyAmount to PlayerPrefs, you need to add score to the value already stored there.

Something like

 int accountBalance = PlayerPrefs.GetInt( "moneyAmount" ) ;
 accountBalance += score ;
 PlayerPrefs.SetInt( "moneyAmount", accountBalance ) ;
Comment
Add comment · Show 5 · 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 vincentamato · Dec 24, 2014 at 05:54 PM 0
Share

@Victory Dan Greene Hi. Thank you for responding. It is much appreciated. Anyways, your methods works perfectly!! The only flaw is that if the score is over 2 it will start increasing the money amount by a certain number.

  1. +1

  2. +3

  3. +6

  4. +10

So for example if I score a 3 my money amount will be 3 plus 3 so it will be 6. Any ideas as to why this happens?

avatar image Victory Dan Greene · Dec 24, 2014 at 06:55 PM 0
Share

It sounds like you're running the "save" routine more than once... $$anonymous$$ake sure you're only running that once, when the play session ends?

(edit) alternately, maybe you're adding score to the "moneyAmount" PlayerPrefs every time the player gets a point? In that case, you want to add 1 ins$$anonymous$$d of score.

avatar image vincentamato · Dec 24, 2014 at 07:10 PM 0
Share

@Victory Dan Greene Hi. Thank you for your response. I would like to extend my sincere apologies, but am so confused and I am such a newb. How would I do these things?

avatar image Victory Dan Greene · Dec 24, 2014 at 07:25 PM 0
Share

One idea is, just before the line

 accountBalance += score ;

add the line

 Debug.Log( "Score was " + accountBalance + "... adding " + score  ) ;

And keep an eye on your console to see when this routine is being run.

avatar image vincentamato · Dec 26, 2014 at 03:56 AM 0
Share

@Victory Dan Greene Hi. Sorry for the delayed response. Can I contact you after the holidays via this question post? It would mean a lot. Thank you very much and have a very nice holiday!

avatar image
0

Answer by HYPERSAVV · Dec 23, 2014 at 10:57 PM

You should check to see if moneyAmount is set, if not set it to score. If it is set, fetch that value and set moneyAmount to that value plus score.

Comment
Add comment · Show 4 · 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 vincentamato · Dec 23, 2014 at 11:17 PM 0
Share

@HYPERSAVV Hi. Thank you for answering. I am kind of a newb so how would I check to see it is or is not set?

avatar image HYPERSAVV · Dec 24, 2014 at 07:42 AM 0
Share

Refer to the documentation for PlayerPrefs

 if(PlayerPrefs.Has$$anonymous$$ey("key"))
 {
    // That key is set, do something
 }

In your case though, this will technically work whether you check if the key exists or not. GetInt will return 0 by default if the key does not exist (or any other value defined as the second parameter). For your program 0 would be an acceptable score for someone who has never played. There are other scenarios where we would want to know if we have set a key or not, which you could use this for. Something to keep in $$anonymous$$d for later :)

avatar image vincentamato · Dec 26, 2014 at 03:55 AM 0
Share

@HyPERSAVV Hi. Sorry for the delayed response. Can I contact you after the holidays via this question post? It would mean a lot. Thank you very much and have a very nice holiday!

avatar image HYPERSAVV · Dec 26, 2014 at 04:44 AM 0
Share

@Vinnya124 sure thing

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

27 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

Related Questions

Money System Not Working Please Help!!!! (Code fully commented!) 3 Answers

A node in a childnode? 1 Answer

How to write & read a string into a text file? 1 Answer

Why can't I change another script's variable with this script? Thanks, 1 Answer

Things to know about 2D games! 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