I’m trying to instantiate buttons inside of a grid layout group at runtime, and my assumption is that object.SetParent(grid,false) will automatically position each button with the correct cell spacing:
public GameObject buttonPrefab;
public override void MenuEnabled()
{
GridLayoutGroup myGridLayoutGroup = GetComponentInChildren<GridLayoutGroup>();
for (int i = 0; i < 5; i++)
{
GameObject tempObj = Instantiate(buttonPrefab, Vector3.zero, Quaternion.identity);
tempObj.transform.SetParent(myGridLayoutGroup.transform, false);
}
}
This all looks fine to me, and these objects do spawn and child themselves to my grid, but they don’t align to its cells: every single button’s local transform remains at vector3.zero. A quick google seems to show everyone else doing it the same way I am, is there some kind of initialization I’m missing which instructs the grid layout group to properly orient its new child?