I need to add X objects to a scrollview. I do it like this:

GameObject statsObject = Instantiate(instance.statsObjectPrefab);

statsParent is the content container if the scrollview.

---- Viewport
-------- Content // This is statsParent

In the pause I see that then objects are added successfully, but they all overlap and no scrollbar is visible.

The content has a content size fitter and a vertical layout group, but I tried now all settings and the result is always the same.

enter image description here

I use Unity 2019.2

  • \$\begingroup\$ Best way to work with that is to use layouts. like grid layouts as simply set right size for contents and you dont need to care for anchors after instantiation \$\endgroup\$ – virtouso Sep 13 '19 at 21:49
  • \$\begingroup\$ Presumably you tried SetParent(statsParent.transform, false)? \$\endgroup\$ – DMGregory Jun 21 '20 at 2:03
  • \$\begingroup\$ Did you try LayoutRebuilder.ForceRebuildLayoutImmediate after adding the object? \$\endgroup\$ – Safa Jun 21 '20 at 9:13

From the Vertical Layout Group docs:

The Vertical Layout Group component places its child layout elements on top of each other. Their heights are determined by their respective minimum, preferred, and flexible heights according to [...]

You have to add a LayoutElement component to your prefab and set its values according to your needs.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.