Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 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 something 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 at 06:09 PM 0
Share

Could you also post the code in which the 'money' gets updated?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 04 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 something. 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 chinese characters or emojis that would make it even harder to understand anything :)

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 at 01:03 PM 0
Share

firstly, when writing the question I meant to write cN, and the money is increasing via the add money function (I checked via Debug.LogFormat the value of the money variable and it went up every time the button was clicked, but i=did not update value in the if (cB == true) function (also checked via the Debug.LogFormat as the value of money in that part of the code remained 0) And as far as the abbreviations, this is the 7th time I rewrote this code, so I shortened it for my own sanity.

avatar image Caeser_21 Syrup_Bandit · Apr 05 at 01:07 PM 1
Share

for your own sanity it (somewhat) makes sense to shorten stuff up. But, when you are going to post your code online where the other person/people have no background knowledge on the code, its hard for them to understand... maybe just for the code online post the full forms :)


So it would be more helpful to post the full forms of the values online

avatar image Bunny83 Syrup_Bandit · Apr 05 at 02:20 PM 0
Share

the money is increasing via the add money function

That method is nowhere to be seen then? If that's the case, you most likely are dealing with two or more instances of the same class. A common mistake is to directly wire a button to the prefab itself instead of the object in the scene. Prefabs are also valid objects which are loaded in memory. So you can call methods on prefabs, but those are seperate from instances in your scene. So you may have more than one instance and you call one method on one instance and the other method on the other instance.


When you're logging to the console and you are not sure where the log comes from, just add a second context argument to your log. When you passed a context object to the Log class, when you click on such a log message in the console, the Unity editor would highlight / ping the context object in the hierarchy or project panel. This gives you direct information what object you're actually dealing with. Note since you use LogFormat and LogFormat has a params array at the end, the version that takes a context object requires the context object to be the first argument. You may simply pass this or gameObject as context. Do this with both of your log statements (the place where you increase your money and where you expect it to be not 0) and you should see which objects you're actually dealing with.

avatar image Syrup_Bandit Bunny83 · Apr 05 at 02:43 PM 0
Share

Originally I had that problem, but with the current code, instead of wiring it to a specific button, I use bools, and on each button I enable a certain bool corresponding to the button. The problem I am seeing, is coming from the money variable not transferring it's value to the if (cB == true) function .

if (cB == true) { Debug.LogFormat("money = {0}", money); if (money >= 10) { buyCow(); } else { Debug.Log("money < 10"); }

even thought the money is being added in a lower function it's value does not increase in the above function. and it is important to note that the bool solution works in the money situation, the only problem is the variable's value not translating.

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.

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

Why is my game connecting to config.uca.cloud.unity3d.com on startup after recent Unity update? 2 Answers

Unity Update- I cannot open my project now 1 Answer

Set integer to new random value every time Update() runs 1 Answer

Project broke when Unity updated 1 Answer

update unity web player 2 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