Create a list of buttons through code (For server browser)

Hey everyone!

For my game, I need to be able to be able to populate a scroll-able area with buttons. Using the old GUI system, I would just do the following inside a GUILayout scrollview:

foreach (RoomInfo room in PhotonNetwork.GetRoomList())
{
    if(GUILayout.Button(room.name))
        PhotonNetwork.JoinRoom(room.name);
}

Even though it looked bloody hideous, it got the job done. Now - using the new GUI - I’m wondering how I can reproduce this. Thanks for any help you guys can provide!

ps. I’m definitely a programmer, not an artist or UI designer so forgive my lack of knowledge in this area :stuck_out_tongue:

   public Transform panel_target;//this must have Grid Layout Group
   public GameObject mybutton;
   GameObject temp;
   // Use this for initialization
   void Start () {
   
     foreach (RoomInfo room in PhotonNetwork.GetRoomList())
     {
       temp = (GameObject)Instantiate(mybutton);
         temp.transform.parent = panel_target_target;
     }
   }

Thanks! I’m going to test this out now.

Edit: Thanks so much for the help! It works perfectly. See the post below.

There is some really odd behavior when I set the parent of the instantiated to the panel. It should look like this (What I get when I place it not at runtime in the editor)


Instead (When I set the parent using transform.parent = x) it looks like this

Sorry for the formatting on this post…

Hi, does using the transform.SetParent() function work for you?

As said by Rune:

1 Like

Aha, Thank you! This works like a charm.