#GridLayoutGroup Works in Editor but Breaks at Runtime (Buttons Overlapping When Instantiated)

1 messages · Page 1 of 1 (latest)

daring nacelle
#

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.

unkempt mesa
#

What does calendarDay.Initialize(dayNumber, this); do?

daring nacelle
#

calendarDay.Initialize(dayNumber, this); is basically the setup step for each button in your calendar. When the button gets created, this line tells it which day it represents and gives it a reference back to the CalendarManager so it knows who to talk to when it’s clicked.

Most Initialize() methods for UI buttons do things like:

Store the day number internally (so the button knows it’s “Day 1,” “Day 2,” etc.)

Update the text on the button so it displays the correct number

Register the button’s click event so that when you press it, it calls the right method on the CalendarManager (usually something like OnDayClicked(dayNumber)).

unkempt mesa
#

I mean yeah I know what I would expect it to do. I'm wondering if you could share the code for that.

daring nacelle
#

Oh i misunderstood kk gimme a sec

stone lava
#

Does any child have a warning labeled The parent has a layout component and that this component shoulld not be here or something similer?

#

if parent has a layout group and child has contentsizefitter, aspect ratio fitter etc. then they dont go well together

#

your best best is to use Canvas.ForveUpdateCanvases()

#

otherwise my recommendation would be to remove the component from the child all together

stone lava
rose fulcrum
#

A layout group can set child width + height from their preferred size. Meaning a content size fitter should not be used on the children

brisk ocean
#

Correct. ContentSizeFitter only ever belongs on an object whose size is not controlled by its parent