I am implementing an advent-calendar feature in Unity using a GridLayoutGroup to arrange 25 day-buttons. When the scene is not running, the layout looks correct and the grid displays exactly as expected. However, when I enter Play Mode and generate the buttons at runtime, the grid breaks completely: all buttons overlap in the upper-left corner, as if the GridLayoutGroup were not applied at
all.
Here is what I have already verified:
The GridLayoutGroup is on the parent object (calendarGrid).
The button prefab has a clean RectTransform (reset, uniform scale, center anchors).
The calendarGrid object also has a reset RectTransform (anchors set to stretch, pivot centered, scale 1-1-1).
The grid works perfectly in the editor, but after instantiating prefabs at runtime, every button snaps to the same position.
The UI Canvas uses standard settings (Screen Space – Overlay).
I am not using any ContentSizeFitter, LayoutElement, or nested Layout Groups on the prefab.
I have tried regenerating the grid before and after canvas activation—no change.
The button creation code looks like this (simplified):
foreach (Transform child in calendarGrid)
Destroy(child.gameObject);
for (int i = 0; i < totalDays; i++)
{
GameObject dayButton = Instantiate(dayButtonPrefab, calendarGrid);
calendarDay.Initialize(dayNumber, this);
}
The moment I run the game, the grid spacing and cell alignment are ignored and all children overlap. It appears that the layout calculation is not updating after instantiation.
My questions:
What causes a GridLayoutGroup to function correctly in the editor but fail at runtime?
Do I need to force Unity to rebuild the layout manually after instantiating UI elements?
Could a hidden parent component be overriding the layout during Play Mode?
Is there a known interaction issue between GridLayoutGroup and inst antiated prefabs?
Any insights from people who have fixed this exact behavior would be appreciated.