• 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 Syrup_Bandit · Apr 04, 2022 at 03:59 PM · update problem

Public variable not updating

My problem is that even thought the value of money is updating in it's own function, the value of money will stay zero in the "if (cB == true)" function, I have tried many suggested solutions but the value of money still won't update. I'm probably missing somet$$anonymous$$ng obvious since I'm still new, any help is welcomed, thanks in advance.

 public class moneyV2 : MonoBehaviour
     {
         public bool cB;
         public bool cCB;
         public bool sCB;
         public bool mB;
 
     public int money;
     public int cN;
     public int cCN;
     public int sCN;
     void start ()
     {
         cN = 1;
     }
     public void OnMouseDown ()
     {
         if (cB == true)
         {
             Debug.LogFormat("money = {0}", money);
             if (money >= 10)
             {
                 buyCow();
             }
             else
             {
                 Debug.Log("money < 10");
             }
         }
         if (mB == true)
         {
             money += cN;
             Debug.LogFormat("money = {0}", money);
             
         }
     }
     public void buyCow ()
     {
         money = money - 10;
         cN++;
         Debug.LogFormat("cN = {0}", cN);

Comment
Add comment · Show 1
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 Caeser_21 · Apr 04, 2022 at 06:09 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 04, 2022 at 10:37 PM

Well, it's not really clear why you mentioned your "cB" variable as the code related to it won't increase money, just decrease it when you buy somet$$anonymous$$ng. You could potentially increase money when mB is true. However your actual issue is most likely that cN is zero so you never gain any money. Why cN is zero? Because you named your "Start" method "start" so it won't be called at all, at least not by Unity. The method has to start with a capital letter.


Apart from that you really should work on giving your variables descriptive names. Sorry but cB, cCB, sCB, mB, cN, cCN, sCN makes absolutely no sense. Why don't you use some c$$anonymous$$nese characters or emojis that would make it even harder to understand anyt$$anonymous$$ng :)

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 Syrup_Bandit · Apr 05, 2022 at 01:03 PM 0
Share
avatar image Caeser_21 Syrup_Bandit · Apr 05, 2022 at 01:07 PM 1
Share
avatar image Bunny83 Syrup_Bandit · Apr 05, 2022 at 02:20 PM 0
Share
avatar image Syrup_Bandit Bunny83 · Apr 05, 2022 at 02:43 PM 0
Share

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

134 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 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 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 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 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

New Update Text Issue 0 Answers

Migration from Unity 5.5.0f3 to Unity 2017- error and HoloLens app not building 0 Answers

Finding object in update function not working. 0 Answers

The new Unity Update Messed Up My Project a bit, Please help? 2 Answers

Everytime I try to add a new component since the last update it keeps saying: "the module which implements this component type has been force excluded in player settings". 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