- Home /
Toggle Button State On/Off
When I attempt to modify the variable "isOn" (On a Toggle) to either true or false after clicking on a toggle button, the Unity Editor becomes completely unresponsive and practically crashes.
When my scene first loads I modify Toggle Buttons to either an on or off state depending on what settings are saved in memory. When attempting to change these values, Unity throws thousands of errors and gives a StackOverflowException. My code that is throwing the errors can be viewed here http://pastebin.com/9ysYyKcX. You can figure out what lines are having issues in the Editor log that is attached to this post. Download the Editor.log: http://goo.gl/IIJufa
I am using Unity 5.0.1.
Thank you for the support!
Answer by viesc123 · Jun 03, 2020 at 03:25 PM
Hey there.. notice that its already some time, since this has been asked, but I just came across a similar issue, so I'll try and answer this: The problem here is that changing Toggle.isOn will also call the OnValueChanged callback of the toggle. Assuming that this kicks off a chain of method calls that lead back to your initial call to Toggle.isOn, you'll end up in a nice endless recursive loop that will freeze Unity (or lead to a StackOverflowException if you're lucky). What you need to use instead, if you want to change the visual part of the toggle without the callback, is Toggle.SetIsOnWithoutNotify(bool isOn).
This method is for some reason is not listed in the script ref under Toggle.. I actually came across it, when I wanted to implement an extension method for toggle that did this.. and then found out that there is already a method with the same name XD
Answer by johnnikson2012 · Feb 11, 2021 at 05:51 AM
Still face those who just add parent toggle group component and drag it in to child toggle group it will prevent to active only one toggle at a same time