0
\$\begingroup\$

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

GameObject statsObject = Instantiate(instance.statsObjectPrefab);
statsObject.transform.SetParent(statsParent.transform);

statsParent is the content container if the scrollview.

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

\$\endgroup\$
  • \$\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 at 2:03
  • \$\begingroup\$ Did you try LayoutRebuilder.ForceRebuildLayoutImmediate after adding the object? \$\endgroup\$ – Safa Jun 21 at 9:13
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$

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.