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!

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.

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;
}

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);    
         }
     }