- Home /
Global Variable Resseting By Itself
So as the question title says I have a script with a "global" boolean variable and it gets set to false (the declaration value) without being set back to it.
A small example below: (by the way I am using C#) (also
private bool gameStarted = false;
void Update() {
if(gameStarted == false){
buildGame();
}
if(gameStarted){
//...
}
}
void buildGame() {
//...
gameStarted = true;
}
I even put print() before and after each reference of gameStarted and it displays that at the beginning of update gameStarted is false with gameStarted being true at the end but on the very next update call it is false again with no instance in the code (even using shift + f12 to find all references) of anything setting the variable back to false. Does anyone know what I should do? Because other than restarting and going bit by bit through the code I don't see any way of knowing why this is doing it.
Edit: sorry if the formatting is bad it wont let me make it look better / I don't know how to let it let me make it look better.
To format your code, paste it in as normal and the hit the 101010 button in the toolbar to format it as code. What this does is ensure that all code is indented by 4 spaces (or you can use 1 tab).
Something else has to be changing it! Can you post the whole of this code? Any reason why you are not doing buildGame in Start?
@blaz0135 you were using block quotes to format the code; >>>. You may have pressed the wrong button. The correct one is the 1010101 one. I formatted the code for you.
@Jamora I tried to use the numbers and it didn't look right so I tried to use the blockquote (") and I guess made it worse. Thank you for fixing it though.
@whydoidoit The problem with posting all of the code is I am untrained with separating the code into multiple scripts so everything is in 1 script and there is alot. The reason I wasn't using it in Start was again with the one script I was building it to where the menu screen would be built and shown from start and then once from there the game has been selected to start the game would then build the level.
Answer by jasperstocker · Mar 09, 2014 at 03:14 PM
You might want to Serialize that private field.
http://docs.unity3d.com/Documentation/ScriptReference/SerializeField.html
TLDR:
[SerializeField]
private bool gameStarted = false;
Unfortunately no that didn't do anything but when used it did stop the boolean from being set to false at all, beginning and later.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135802im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Issues sharing variable between 2 scripts 2 Answers
How can I reset a rigidbody? 1 Answer