- Home /
 
instantiate a button in a panel in new uGUI
I have a List in a script which is being increased every now and then. I would like to instantiate a button in a panel with a grid layout for every string in the list. Something like:
 foreach(int i=0; i < list.Count; i++){
     instantiate(button);
     button.text.text = list[i]; 
 }
 
               I'm not sure how to say it's got to be in the panel though.
Answer by Kirbyrawr · Aug 24, 2014 at 02:38 AM
I think you want to put them inside the layout right?
If it's right, here's the code:
  public GameObject yourGridLayout;
     
     //Here i put on click but you need to put your method.
     void OnClick(){
     
     for(int i = 0; i < list.Count; i++){
     
      var buttonInstantiate = Instantiate(button);
      buttonInstantiate.text = list[i];
      buttonInstantiate.transform.gameObject.parent =   yourGridLayout.transform;
     
     }
     
     }
 
               Hope it helps, remember that i missed the class on the script.
Your answer
 
             Follow this Question
Related Questions
Multiple Prefab Instantiation, Camera, Button and Scroll View Question 1 Answer
Buttons not showing up the first time the menu loads, but showing up on all menu openings after 2 Answers
Bearded Multi-Headed Instantiation Dragoon Question 1 Answer
How to Instantiate from correct Icon. 1 Answer
Error attempting to change text on instantiated button 0 Answers