- Home /
Update Function Won't Do If Statements
We have a static var bCounter. bCounter is initialized to 0, and will sometimes be incremented in another function. Every time our script updates it goes through a serious of ifs to determine what to do based on bCounter. However, our script's update function won't even recognize when bCounter is 0 (which we are testing for using the print statements.) Why won't the update even do the first if statement?
function Update ()
{
if (bCounter == 0)
print("Still this far!");
else if (bCounter == 1)
{
print("Good job!");
if (bThirteen == true && Time.time - temp13 < 1)
{
Stair13.transform.Translate(0,Time.deltaTime * 13.73371,0);
}
else if(bThirteen == false)
bCounter = 0;
}
Impossible to tell unless you tell us when, how, and under which conditions bCounter is incremented/modified.
For starters, put print("Time "+Time.time+", bCounter="+bCounter);
at the very beginning of this Update().
Also remember that if this is an Editor script or has "ExecuteInEdit$$anonymous$$ode" set, your static var will never be reset.
Here is when bCounter is incremented. This is in another script. We have checked within this script to see if bCounter's value is changed, and it does. (in the update function for this script we had an if statement that would print if it was equal to 1)
function On$$anonymous$$ouseDown() { transform.Translate(0,0,-.3); if (NumberButton.bThirteen == false) { NumberButton.bThirteen = true; NumberButton.temp13 = Time.time; NumberButton.bCounter += 1;
} }
Is the increment function in a different script? If so, the other script may be only making a copy of the variable, not transferring the value to your Update function.
Answer by Wolfram · Jan 12, 2013 at 01:24 AM
To prevent the problem of forgetting to attach a script with static variables to a scene object (which can happen since the static variable "exists", even if the script is not in the scene), instead of using static variables create a "Manager" or "Controller" script, put it exactly once in your scene, and move all these globally accessible variables to that manager. Then make these variables non-static.
Your answer
Follow this Question
Related Questions
only updating number vars once 1 Answer
what does Static Function exactly do? 2 Answers
How to use same code at multiple scenes ? 1 Answer
If statements in update 1 Answer
How can I preserve static object/data between editor and play? 2 Answers