0
\$\begingroup\$

I'm loading prefabs into a content area using a content size fitter and vertical layout group. I've tried multiple things to get the loaded prefabs to stretch across the entire content size (including the Unity Manual for this issue), but I'm at an impass. I'll post a screenshot below to show what I'm working with:

enter image description here

Here's how I'm loading in the prefabs in case this is where i'm screwing up:

void CreateRaceSelect() {

 for (int i = 0; i < races.races.Count; i++) {
     raceButton = (GameObject)Instantiate(raceButtonPrefab);
     raceText = raceButton.transform.FindChild("Text").GetComponent<Text>();

     raceButton.name = i.ToString();
     raceButton.transform.SetParent(content.transform);
     raceButton.GetComponent<RectTransform>().localPosition = new Vector3(xPos, yPos, 0);
     raceText.text = races.races[i].ToString();
     yPos -= (int)raceButton.GetComponent<RectTransform>().rect.height;
    }
 }

I'm sure this is a minor screw up but at this point I have no other ideas.

\$\endgroup\$
  • \$\begingroup\$ I see you have selected "stretch" on one of the Rect transforms. Did you try holding down alt when you did this? The option natively changes the anchor; you need to hold alt (cmd on Mac) to change position, and without doing so, stretching does not seem to have an effect. \$\endgroup\$ – Gnemlock Dec 6 '16 at 1:07
  • \$\begingroup\$ I have the content set to stretch across the entire width of the View Area. Checking the Anchors, the 4 anchor points are distributed to each corner of the view port \$\endgroup\$ – Adariel Lzinski Dec 6 '16 at 1:15
  • \$\begingroup\$ That is not what I asked. When you change the anchor, it does not appear to enforce stretching. You need to hold alt / cmd and change the anchor, to "Also set position". Note that it will show you "stretch" as being selected without this. The fact that the anchors are set in any way means nothing in the context where I have used image/gui stretching. \$\endgroup\$ – Gnemlock Dec 6 '16 at 1:32
  • \$\begingroup\$ Yes I tried that as well. \$\endgroup\$ – Adariel Lzinski Dec 6 '16 at 1:33
  • \$\begingroup\$ Can you clarify what object should stretch where? I don't really get what you're asking here. Also, in a case like this, the file structure is really important, since stretching is a parent-child relationship. \$\endgroup\$ – John Hamilton Dec 6 '16 at 6:46
1
\$\begingroup\$

Check the Child Force Expand Width/Height According to your requirement.And set value of spacing in vertical layout component to zero instead of -6. I guess this will solve your issue.

\$\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.