- Home /
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! :)
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 ) ;
@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
+3
+6
+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?
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.
@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?
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.
@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!
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.
@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?
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 :)
@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!