Search Unity

Custom Toggle Group Behaviors

Discussion in 'UGUI & TextMesh Pro' started by Nikovich, Aug 20, 2015.

  1. Nikovich

    Nikovich

    Joined:
    Jul 10, 2010
    Posts:
    69
    I have a collection of buttons. I wanted these buttons to behave with the following rules:

    1. Each button corresponds to a video which plays when the button is clicked.
    2. The button stays in its on state when clicked.
    3. When the animation is over, the button reverts to its idle state.
    4. If another button is clicked, that button's video plays, the button goes to on state. All other buttons revert to idle.
    5. If an "already on" button was clicked, its video would restart and the button state would not change (it would remain in on state.

    However, trying to manually change the button's states manually was becoming a headache. Due to the way they have to be loaded in and the fact that the kiosk it will be on will be touch controlled, there were problems trying to change the button states, so I changed them to toggles. I figured I'd set up a loop that goes through the array of toggles, sets their isOn property to false, then set the clicked one's isOn to true, just like I would do a radio button set in Flash.

    Problem is, trying to manually affect isOn causes Unity to explode. I get a StackOverflowException after a long period of inaction.

    I thought that adding these all to a toggle group would help. While it did automatically set all other buttons but the clicked one to highlighted, it also deactivated it if clicked again. Other than that, it gave me the ability to only have one active button at a time, and I was still able to deactivate it when the animation was over. I tried to solve this by telling whatever toggle was clicked to always set its isOn to true, but once again, it caused Unity to explode.

    I feel like I'm missing something obvious here; this kind of behavior should be easy to achieve. Would it be easier to go back to making them buttons? Or is there a better way of handling toggles I haven't tried?