When I hit play in the editor I want to load an int from playerprefs to determine if the music should play. If the user toggles a Toggle then I start or stop the music and update the playerprefs then save them.
However, when I exit the application the Toggle gets a value change message and the isOn value will not be set to whether the user change it or not, it will be always set to the initial value of the toggle.
This means if the user exits the app while a toggle is active it screws up the prefs.
Any workarounds?
I know this is four years late, but I may have an answer. It’s hard to tell if your problem is the same from the information your provided, but maybe this will help.
When I set the toggle states in “start”, it automatically triggers the “On Value Changed” event, if the new toggle bool value is different than what was set in the inspector. This calls a method I had attached in the inspector, which caused undesirable results.
I found a solution. I don’t know if it’s the best solution, but it seems to work without any issues. I’ve outlined my problem and solution in more detail here. Undesirable call of Toggle's "On Value Changed" when "isOn" is changed through script - Unity Answers
The tl;dr is that I removed the method call from the “On Value Changed” Event, in the inspector, and then added an “Event Trigger” Component with a “Pointer Click” event and used that instead.