- Home /
Simplest way to attach toggles to booleans, maybe some kind of reference variable?
I am trying to link a Toggle to a boolean value on another object. To do this, I know I can make a function on the other object, call it SwitchBool1, and call it from the OnValueChanged on my Toggle. But now I want to make another toggle for Bool2, and I don't want to make a separate SwitchBool function for every bool/toggle combination. So instead, I'd like my Toggle to attach directly to Bool1 and another Toggle attached directly to Bool2. Is this possible? I believe that with C#, I can't store a reference variable to OtherClass.Bool1 on my ToggleButton class, but if anyone knows definitively or has a reasonable alternative, I'd love to hear. So far I see two options that both get the job done but neither is as clean as a stored reference variable would be:
Create a function that updates every boolean from its toggle and call the same function from every Toggle(UpdateBoolsFromToggles or something). Should be fast enough, but means I have to manage the bool/Toggle linkage in code and create a Toggle reference alongside each bool. Mess.
Create a SwitchBool function declaration for each bool I'd like to toggle and then call each from its appropriate toggle. Means I can manage the bool/Toggle linkage in Unity but lots of function clutter. Mess too!
Any advice would be swell, thanks much!
Your answer
Follow this Question
Related Questions
Use a toggle button to change a GameObjects visibility 2 Answers
[4.6] Toggle/Checkbox On/Off 1 Answer
Toggles/Toggle Groups Question 0 Answers
How to make an UI element clickable based on its fill amount? 1 Answer
Unity UI Button Switch Bool? 2 Answers