- Home /
Toggle OnValueChanged ignore on start
So I have an UI.Toggle on my video settings menu that toggles a SSAO-bool and therefore enables/disables the SSAO script. I dropped the ToggleSSAO() method on OnValueChanged-field, so it gets called, when its bool "isOn" has changed.
public void ToggleSSAO(){
ssao = !ssao;
GetComponent<UnityStandardAssets.ImageEffects.ScreenSpaceAmbientOcclusion>().enabled = ssao;
BoolPrefs.SetBool("SSAO", ssao);
}
I have used Eric's BoolPrefs script that is simulating bool values to save them as ints in the PlayerPrefs class.
Anyways, at start of the game, the saved ssao-bool should initialize the ssao bool itself and the isOn bool on the UI.Toggle.
void Awake() {
ssao = BoolPrefs.GetBool("SSAO", true);
GameObject.Find("Toggle_SSAO").GetComponent<Toggle>().isOn = ssao;
}
The problem is that OnValueChanged() therefore gets called at the beginning of the game because of this initialization and switches the ssao-bool again, so the isOn bool is inverted. How can I resolve this?
Answer by ElijahShadbolt · Oct 17, 2016 at 04:43 AM
Change the method to look like this:
public void ToggleSSAO(bool newValue) {
ssao = newValue;
//GetComponent...
}
Drop the Dynamic bool ToggleSSAO method in the OnValueChanged event instead of the Static Parameters ToggleSSAO. You should see no check box in the event. This way whenever the Toggle's isOn
is changed, it will also set ssao
.
Answer by Wiechering · Mar 06, 2020 at 11:08 AM
This is very old, which i know but somehow after googling another topic i ended up here. For anyone else having the described problem:
Just call Toggle.SetIsOnWithoutNotify( state );
This can be used but it still wouldn't make much sense as such kind of program$$anonymous$$g is dangerous and error prone. $$anonymous$$eeping track ot the same state in two different places simultaneously just makes no sense. If you use a toggle (which has an internal state) it should represent your setting and you just apply that state to the thing you actually want to set. Blindly toggling whenever a change happens just makes no sense.
Your answer
Follow this Question
Related Questions
playerprefs dont load for menu toggle 0 Answers
Toggle bool with keypress -2 Answers
bool field initialization ignored 0 Answers
Save a Toggle Group state? 0 Answers
Using an Enum for GUI.Toggle? 1 Answer