- Home /
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);
Could you also post the code in which the 'money' gets updated?
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 :)
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.
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
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.
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.