- Home /
Boolean Trouble, Please help....Boolean does not flip another boolean
Script A checks every frame to see if Script B has a boolean true, so that script A can flip its own boolean, every frame. Script B flips the bool, script A logs a debug line implying that it caught the flip, but the action which is supposed to trigger when the bool is true, (which would un-flip it afterwards) does not happen. Setting the boolean on script A manually, does trigger the action, just no idea why it would debug that and then not actually flip it.
"BS" is a script which, Clears player preferences, then re-saves a lot of structures, when a boolean, "saving", is true. When BS is done saving, it flips "saving" back to false.
I need "Slot" to recognize that "saving" has been flipped, so that "slot" can flip its own boolean on, called "flip", so that the slot can re-run its SaveItem function, which would be turning "flip" back off.
I can only get the slot to recognize that "bs.saving"
Here is the Update from the Slot script:
public void Update(){
if (bs.saving == true) {
Debug.Log ("See BS SAVING true on slot"+gameObject.name);
flip = true;
Debug.Log ("Set FLIP to TRUE because BS was SAVING"+gameObject.name);
}
if (flip){
Debug.Log ("WE ARE FLIPPED, ARE WE OCCUPIED?"+gameObject.name);
//SaveItem ();
if (slotOccupied == true) {
Debug.Log ("FLIPPED and OCCUPIED now SETTING BOOL AND SAVEITEM"+gameObject.name);
PlayerPrefsX.SetBool (gameObject.name + "Bool", true);
SaveItem ();
}
}
}
Here is the update from the BuildingSaver script: void Update(){
if (saving) {
PlayerPrefs.DeleteAll ();
buildings = 0;
PlayerPrefs.SetInt ("howManyBuildings", 0);
saving = false;
SaveAll ();
}
}
Hello @$$anonymous$$-Hawk I think you may have an update ti$$anonymous$$g problem here. You are trying to use at least 3 booleans there (saving, flip and slotOccupied) and 2 of them you are not even showing at what point in your code they're set, (slotOccupied not set to true or false and flip is never set to false, at least in the portion of the code that you are showing) so I can't really know what's happening without taking a look at the bigger picture.
I don't really like dabbling in boolean hell, so I have a suggestion. If you could tell me what are you trying to achieve with this whole saving system, I could try to come up with a different approach to achieve the same thing without using so many bools and update methods, and with the added benefit of keeping sanity in healthy levels :D trust me. You should stay away from booleans as "event switches", as much as possible.
The execution order of updates is undeter$$anonymous$$ed, so you'll end up with race conditions. It looks like what you're trying to create is a mutual exclusive lock. Take @$$anonymous$$acDx up on their offer to help re-engineer the code.
Your answer
Follow this Question
Related Questions
Coroutines and if statements 1 Answer
Change Bool When Audio Clip Ends C# 1 Answer
PlayerPrefs Problem crashing with SetBool 2 Answers
BoolPrefs Problem 2 Answers
Purchase Level Unlock System 1 Answer