Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Unity UI Scrollview with Instantiated Buttons List Not Working as Intended

Discussion in 'UGUI & TextMesh Pro' started by way3edgy, Dec 11, 2023.

  1. way3edgy

    way3edgy

    Joined:
    Feb 6, 2014
    Posts:
    30
    Hello,

    I'm trying to use scrollview for a list of instantiated buttons. I've been using OnGUI for this and it works fine, but I have to stop using OnGUI for reasons. I cannot get the buttons to go inside the scrollview. If I set it up before runtime it works, but instantiated buttons don't. Here is the test code:


    for (int qq = 0; qq < 20; qq++)
    {
    tmpBtn=Instantiate(loadButton,loadVerticalGroup.transform.position,loadVerticalGroup.transform.rotation);
    tmpBtn.transform.SetParent(loadVerticalGroup.transform);
    tmpBtn.GetComponentInChildren<TextMeshPro>().text=qq.ToString();
    }
    loadScrollview.GetComponent<ScrollRect>().content=loadVerticalGroup.GetComponent<RectTransform>();


    Where loadScrollview is the Scrollview, loadVerticalGroup is the viewport child of the scrollview, and tmpBtn/loadButtons are the instantiated buttons.

    The hierarchy of objects is loadScrollview > loadVerticalGroup > loadButton(s).
    The loadScrollview has components Scrollrect and Content fitter.
    The loadVerticalGroup I have tried with and without Vertical Layout Group.

    The buttons show as a list, but just drop endlessly off the screen. Like this...

    https://postimg.cc/3WtVjHf1


    Am I making a noob mistake here?
     
    Last edited: Dec 11, 2023