Setting toggle group on instantiated toggles

So I am making an inventory system, my current method involves toggles which will be instantiated as children of the toggle group. My question is how can I set the group of an instantiated toggle in a script?

Current script:

public GameObject slot;
public int gap = 2;
public int quantity = 2;
RectTransform slotTransform;

void updateDisplay () {
        for (int i=1; i<=quantity; i++) {
            slotTransform = slot.GetComponent<RectTransform>();
            slotTransform.localPosition = new Vector3(0,-(i-1)*(2+slotTransform.sizeDelta.y));
            Instantiate (slot).transform.SetParent (transform,false);
        }
    }

So slotTransform is a prefab with a Toggle?
Call GetComponent on slotTransform and set the group using Toggle.group.

var toggle = slotTransform.GetComponent<Toggle>();
toggle.group = GetComponent<ToggleGroup>(); // Guessing you the group is on the same object
1 Like

Tried this and it initially failed. I then realised that I never added UnityEngine.UI to the uses clause.
Thanks for the help anyway!

1 Like