• 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 24, 2014 at 01:21 AM · gamevariablescoremoneysystem

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!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 3 · 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 01:54 AM 0
Share

@jgodfrey Okay so I just replace the if statements with the above???

avatar image awplays49 · Dec 24, 2014 at 03:39 AM 0
Share

You could do if (money == 0) but im not sure if you want that

avatar image Landern · Dec 26, 2014 at 04:10 AM 0
Share

@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(); 
     }
 }
avatar image
0

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;
 } 
Comment
Add comment · Show 1 · 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 01:34 AM 0
Share

@Wildan $$anonymous$$ulbarok Sorry but this didn't really do anything. It is still just displaying the score. Any other ideas?

avatar image
0

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


Comment
Add comment · Show 12 · 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 01:53 AM 0
Share

@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?

avatar image Srki94 · Dec 24, 2014 at 02:04 AM 0
Share

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.

avatar image vincentamato · Dec 24, 2014 at 02:26 AM 0
Share

@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?

avatar image Srki94 · Dec 24, 2014 at 02:58 AM 0
Share

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.

avatar image vincentamato · Dec 24, 2014 at 03:17 AM 0
Share

@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!

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 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

Related Questions

Combined Total of Different Variable Values 2 Answers

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

Score going up every second 1 Answer

A node in a childnode? 1 Answer

Implementing Level ID 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