- Home /
How to Name Individual Buttons Within a List
Hi everyone, I have been trying to figure out a way to name all of my buttons that are within a list individually. I can't quite figure out how to do that. If I try to put text.ToString() within the GUIlayout.button function I get this error GUIlayout.button has invalid arguments.
     public List<int> listOfButtons = new List<int>();
     public string[] Text;
     void OnGUI(){
     Text = new string[]{"Button1","Button2","Button3"};
     for (int i = 0; i < listOfButtons.Count; i++)
     GUILayout.Button(Text.ToString());//Error GUILayout.Button has invaild arguments
     }
Answer by kmeboe · Sep 24, 2012 at 07:18 PM
There are two issues with your code:
1) "listOfButtons" is empty, so iterating over it won't generate any UI buttons. 2) When grabbing the text for each button, you would need to index into the array.
Here's the updated code:
     public string[] Text;
     void OnGUI()
     {
         Text = new string[]{"Button1","Button2","Button3"};
         for (int i = 0; i < Text.Length; i++)
         {
             GUILayout.Button(Text[i]);
         }
     }
Answer by Muuskii · Sep 22, 2012 at 06:53 PM
You might have to create a new data type to hold the information that you need. For instance: (written on the fly)
 public class myButton
 {
     public string text;
     //now put the functions you need for clicking
 }
 //Make sure to fill this :P
 public List<myButton> listOfButtons = new List<myButton>();
 void OnGui()
 {
    for (int i = 0; i < listOfButtons.Count; i++)
         if(GUILayout.Button(listOfButtons[i].text) )listOfButtons[i].OnClick();
         //You can have different functions called for different buttons
 }
Of course that all could be unnecessary and you might just need to use a Hashtable. Can you give some more information on the specifications needed for this project?
Thanks! :3
So you need to pass an array to GUILayout.Button to get a button that has multiple names?
No, I want to name a list of buttons with a string array. But every time I try to insert a string array into GUILayout.Button I get an invaild arguments error. Like this. GUILayout.Button(Text.ToString());//Error GUILayout.Button has invaild arguments
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                