- Home /
UI.Toggle set to isOn initial state on exit
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 have found myself in the same situation and after 4 hours of trying, searching and typing I am close to $$anonymous$$rs, this should be so simple. This is nonsence. I can't believe this has been a bug or a problem for past 3 years. I am using toggle.IsOn to check when the scene loads up at which state toggle should be but it still gives me wrong results. I am using print command at each step of the code and I have noticed that my Toggle function gets called when i depress play button in Unity editor. Something is retarded or I am. If anyone knows what I am doing wrong please do tell me. Cheers
Answer by Fragmental · Jun 19, 2018 at 04:28 PM
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. https://answers.unity.com/questions/1519824/undesirable-call-of-toggles-on-value-changed-when.html
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.
Your answer