I’m saving the user’s value of “isOn” for various toggle states, and then setting those toggle states during start.
Each toggle has a method I want called when the user changes the value of the toggle, but not when the value is changed via script. However, it seems as though the “On Value Changed” event gets called when I change the value through script.
Here is a sample of the code that gets called in start to set the toggle
private void Start()
{
if (PlayerPrefs.HasKey("rememberAuth"))
{
rememberAuth = PlayerPrefsX.GetBool("rememberAuth");
rememberToggle.isOn = rememberAuth;
}
else
{
PlayerPrefsX.SetBool("rememberAuth", rememberAuth);
}
}
And here is a sample of the method that gets called by the “on value changed” event.
public void RememberMeToggle()
{
if(rememberToggle.isOn)
{
SaveAuth();
}
else if (!rememberToggle.isOn)
{
DeleteAuth();
}
PlayerPrefsX.SetBool("rememberAuth", rememberAuth);
PlayerPrefs.Save();
}
Now, if the bool value of the toggle is different from the default value that was set in the editor, the RememberMeToggle() method gets called even though I don’t want it to. This can cause some issues.
I’ve found a solution for this, and that is to remove the method call from the “On Value Changed” Event, in the inspector and then add an “Event Trigger” Component with a “Pointer Click” event and use that instead.
Is this what I’m supposed to do? Is there a better option? Is the function of “On Value Changed” being trigger from the script desirable and intended?
I had a lot of trouble finding a solution through searches, so if I am doing this right, even though I answered my own question, it might help someone else who’s searching.