- Home /
How to set ToggleGroup of a Toggle in C#
Im trying to set a ToggleGroup of dynamically created toggles without success. I have a 3D object that has child object Toggle. I instantiate the object from prefab, but then I would need to set the toggles togglegroup.
I set the desired togglegroup to be used here from editor:
public ToggleGroup toggleGroup;
Then register the newly created toggle to it:
toggleGroup.RegisterToggle(buttonObject.GetComponentInChildren<Toggle>());
But It simply does nothing. No errors or no nothing. What am I doing wrong here? O.o
Thanks in advance :)
Answer by SkaredCreations · Dec 18, 2014 at 01:51 PM
Set the property "group" of your toggles. I'm not sure what RegisterToggle is meant for, but assigning "group" property of the toggles works as expected.
Thanks for nudging me to right direction!
// Variables C#
public ToggleGroup toggleGroup;
private Toggle newToggle;
// Code C#
newToggle = buttonObject.GetComponentInChildren<Toggle>();
newToggle.group = toggleGroup;
Works perfectly, thank you! :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Toggle Group on Instantiated Toggles? 0 Answers
C# Dynamically add components? 3 Answers
On Value changed event fired without the value changed. 1 Answer