Trying to set spacing for verticalLayoutGroup

Hello. I have a vertical layout group with a list of attached images. It works well, a list of objects is created, but the problem is that the spacing between these objects is set at about 200 on the y axis. I cannot change the distance between the objects, I tried settings the padding but that didn’t work either. Here is my code

Image img0 = GameObject.Find(“ImageSlider”).GetComponent();

GameObject UItextGO2 = new GameObject(“Image77”);
UItextGO2.transform.SetParent(img0.transform);

RectTransform trans = UItextGO2.AddComponent();
trans.anchoredPosition = new Vector2(0, 0);

Image img1 = UItextGO2.AddComponent();
img1.GetComponent().color = Color.black;

Color color1 = img1.color;
color1.a = 0.1f;
img1.color = Color.clear;

VerticalLayoutGroup glg = UItextGO2.AddComponent();

glg.padding.left = 2;
glg.padding.right = 2;
glg.padding.top = 2;
glg.padding.bottom = 2;

This code works very well, but the objects that I add to it have a set spacing that I cannot change, can anyone help thanks.

Add a content size fitter and set the spacing on the layoutgroup. That might fix it, but you may have to still adjust some settings

1 Like

Doesn’t work. I tried setting the value of spacing on the verticallayoutgroup but nothing has changed. Any other ideas, thanks. This should not be so hard.

Set it up in the editor first, then set up the code. It’s much easier to tinker around visually until you figure out what makes it work, then access those properties in code. You may need to have the layout items anchored or aligned to a side to prevent them from auto-spacing evenly.

The answer you’re looking for… is to set the parent height to zero

3 Likes

I love you. - 5 years in the future.

1 Like

Please use the like button rather than necroing threads.

Thanks.