- Home /
Toggle Group event called 2 times Unity UI
Why my toggle group event called 2 times on click of Toggle button?
I have created just test scene for this purpose and getting same result.
Here is my inspector image :
Following is Toggle component image :
I have written following code in script :
public void OnTankTypeToggleClick (int tankType)
{
Debug.Log ("click method called : " + tankType);
}
Above debug statement is print on console two times. By this mean, click method is called two time. I want to call click method just single time. So please give some awesome suggestion to me.
I need some suggestions for this. Please someone help me in this.
Okay, here are some more details regarding requirement.
Following image represent raycast details :
Code for script is above only. Just click method exist in full script. I just pass index over each button click argument.
I am getting following output when I click on 1 number toggle.
If you have a toogle group and only one member can be active at a time, switching a toggle "on" makes another toggle go "off". So you have 2 OnValueChanged events and your script on the main camera is called twice (by the toggle script you pressed "on" and the one which is going "off" automatically).
@saschandroid, thanks for your reply. You are right about this. One event is called for "off" and other is called for "on" state. I just want single call then what to do?
See the comments above.
If you want the toggles to be independent then they shouldn't be in a toggle group.
If you don't want them to be independent then you probably want the function to be called on both toggles, but to do different things depending on whether the toggle has been turned on or off. You can do this by checking the value of the toggle in the callback function and acting accordingly.
anyone know a solution? i have this problem always when i use toggle groups. i saw some time ago someone wrote its a bug within unity, is there some workaround? or some fix i dont know about?
That is because on toggle states become OFF and other toggle states become ON. Because of this reason, two times same event gets called.
I believe the solution is in the comments above. It's not a bug: as explained above the function will be called for each button. In a toggle group, some toggles will be turned off when anther is turned on - so they all get a callback.
Exactly how you want to handle this fact will depend on exactly you want to achieve.
Also have converted to comment.
Answers are for answers :)
If the above doesn't help, you're probably best off posting a new question with details of your situation and details of what you think is going wrong.
if(GetComponent().isOn == true)
i just checked it this way. man it was a struggle. i had some other problems as well, but this worked..
Answer by siddharth3322 · Jun 18, 2020 at 05:30 PM
This problem, I was facing because one toggle state becomes OFF and another toggle state becomes ON. So for these both events, the same method will get called.
This is the reason why the same method called two times. This is not a bug but by the choice implementation. You have to put one check condition within the method code so it can only get called one time.