- Home /
Money System Not Working Please Help!!!! (Code fully commented!)
Hi. I recently posted a question sort of like this but this will be different. This code is fully commented and commented well. I want the player's money to be equal to all the point added up. For example if they score a 4 one time and a 2 another time there money value should be six. For every point the player gets they receive a coin. My code looks fine but does not work right. Here it is:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DisplayMoneyAmount : MonoBehaviour {
//Money amount
private int moneyAmount;
//Text that display the money amount
public Text moneyamountLabel;
//score variable
private int score;
//On start (pretty self-explanatory)
void Start () {
//setting the value of score to whatever the score was
score = PlayerPrefs.GetInt ("splatScore");
//if the money amount has not been assigned then...
if (moneyAmount == null) {
//set it equal to the score value
moneyAmount = score;
}
//but if the money has been set then...
if (moneyAmount != null){
//take that money amount and add the value of the score to it
moneyAmount = moneyAmount + score;
}
//change the text of the money amount label to the money amount
moneyamountLabel.text = moneyAmount.ToString();
}
}
Please help! Thanks!
Answer by jgodfrey · Dec 24, 2014 at 01:34 AM
A few things...
Since moneyAmount is of type INT, it will never be "null". Unassigned INTs have a value of "0".
Assuming moneyAmount is being assigned elsewhere, then you can probably replace most of the above with:
moneyAmount += score;
If moneyAmount is unassigned, it'll start as 0 and will therefore be equal to the score after the above. Otherwise, if moneyAmount has a value, it'll be increased by the score value.
@jgodfrey Okay so I just replace the if statements with the above???
You could do if (money == 0) but im not sure if you want that
@jgodfrey is spot on. You're not using nullable types with int. You can if you really want too, but it doesn't seem reasonable given what you're doing. But for completeness, i've mentioned nullable types in the below.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Display$$anonymous$$oneyAmount : $$anonymous$$onoBehaviour {
//$$anonymous$$oney amount
private int moneyAmount;
//Text that display the money amount
public Text moneyamountLabel;
//score variable
private int score;
//On start (pretty self-explanatory)
void Start () {
//setting the value of score to whatever the score was
score = PlayerPrefs.GetInt ("splatScore");
//unassigned int types default to 0, if you did private int moneyAmount = default(int); the value would be zero. For an int type to be null, it needs to be int? or Nullable<int>, but you don't need that
if (moneyAmount == 0) {
//set it equal to the score value
moneyAmount = score;
} else {
moneyAmount += score; // this is the same thing as doing moneyAmount = moneyAmount + score;, it's just short hand
}
//change the text of the money amount label to the money amount
moneyamountLabel.text = moneyAmount.ToString();
}
}
But techincally, you don't need:
if (moneyAmount == 0) {
//set it equal to the score value
moneyAmount = score;
} else {
moneyAmount += score; // this is the same thing as doing moneyAmount = moneyAmount + score;, it's just short hand
}
You only need the else part every time, because 0 + noneZeroValue is all good.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Display$$anonymous$$oneyAmount : $$anonymous$$onoBehaviour {
//$$anonymous$$oney amount
private int moneyAmount;
//Text that display the money amount
public Text moneyamountLabel;
//score variable
private int score;
//On start (pretty self-explanatory)
void Start () {
//setting the value of score to whatever the score was
score = PlayerPrefs.GetInt ("splatScore");
moneyAmount += score; // this is the same thing as doing moneyAmount = moneyAmount + score;, it's just short hand
//change the text of the money amount label to the money amount
moneyamountLabel.text = moneyAmount.ToString();
}
}
Answer by WillNode · Dec 24, 2014 at 01:28 AM
you need to change the if
to else if
if you want to say "but"
if (moneyAmount == null) {
moneyAmount = score;
}
else if (moneyAmount != null){ //<-- Problem is here, script will always pass this if you not add the keyword "else"
moneyAmount = moneyAmount + score;
}
@Wildan $$anonymous$$ulbarok Sorry but this didn't really do anything. It is still just displaying the score. Any other ideas?
Answer by Srki94 · Dec 24, 2014 at 01:40 AM
What exactly is not working? Your script works fine over here when I set some initial values. I also wrote some shortcuts in code and added some test code:
private int moneyAmount, // Just declare this with value to test script
score; // You can declare values like this if they are of
// same type
void Start () {
score = 200;
if (moneyAmount == null)
{
moneyAmount = score;
}
else if (moneyAmount != null)
{
moneyAmount += score; // You can use += to add to value, instead of
// writing moneyAmount = moneyAmount + something
}
Debug.Log (moneyAmount);
// If you don't modify this code, console will write 200
// However, if you add value to moneyAmount on declaration like this :
// private int moneyAmount = 200, // Just declare this with value to test script
// score;
// Console will write 400 instead of 200, meaning that this works fine.
}
void Update()
{
if (moneyAmount <= 3000) { // Just a simple check of addition
moneyAmount++; // You can use ++ to add only one to an integer
Debug.Log(moneyAmount);
}
}
@Srki94 Yes I ran your script and it print to the console 400. But when I modified to to fit my needs the money amount level just displays the score. Here is your code modified to suit my needs:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Display$$anonymous$$oneyAmount : $$anonymous$$onoBehaviour {
public Text moneyamountLabel;
private int moneyAmount = 0; // Just declare this with value to test script
private int score; // You can declare values like this if they are of
// same type
void Start () {
score = PlayerPrefs.GetInt("splatScore");
if (moneyAmount == null)
{
moneyAmount = score;
}
else if (moneyAmount != null)
{
moneyAmount += score; // You can use += to add to value, ins$$anonymous$$d of
// writing moneyAmount = moneyAmount + something
}
Debug.Log (moneyAmount);
// If you don't modify this code, console will write 200
// However, if you add value to moneyAmount on declaration like this :
// private int moneyAmount = 200, // Just declare this with value to test script
// score;
// Console will write 400 ins$$anonymous$$d of 200, meaning that this works fine.
moneyamountLabel.text = moneyAmount.ToString ();
}
}
Why is it just displaying the score?
Because you declare moneyAmount with value of 0. Try to put any other value but 0 there, or if you are getting that value from somewhere else, paste that code so that we can see what's wrong.
@Srki94 Hi. I appreciate your response. Anyways, I not really getting this money amount value from anywhere. I want the player to start out with 0 but for every point they get they also get a coin. I am so very confused! I tried setting the money amount to PlayerPrefs.GetInt("splatScore"); but this does not seem to work. It still just displays the score. Any ideas?
For some reason I can't comment anymore :/ Or at least, this long text ( even tho I have 60 chars left). I tried to use pastebin : http://pastebin.com/nhhUz1wS
If mod figures out why I can't comment, please post it back here.
@Srki94 Hi. Thank you so much for taking the time to right out that code. I really do appreciate it. Anyways, I read over it and I am unsure why we are referencing the high score. I want the high score to be separate from the money amount, right? Or am I just misunderstanding why we need the high score or best score? You may just email me back. $$anonymous$$y email is: vinnyamato64@gmail.com I look forward to hearing from you. Again thank you so much!