- Home /
Making a set of buttons from an Array
Hi guys,
Having done some thorough research on the subject, I cannot work out how to create a set of buttons from an array or list. Here is my code that I have written to, upon clicking a GUI button, create a separate set of buttons with names taken from the array. This however has no effect in my game.
private void OnGUI(){
if (some_bool_to_show_gui) {
GUI.Box(new Rect(10,10,100,90), "Do Things");
if (GUI.Button (new Rect (15, 15, 100, 50), "Do Something")) {
DoSomething (); //This Works
}
if (GUI.Button (new Rect (15, 60, 100, 50), "Do Something Else")) {
for (int i = 0; i < SomeArrayofStrings.Length; i++) {
if(GUI.Button (new Rect (15, 60+15*i, 100, 50),SomeArrayofStrings[i])) {
DoSomethingElse();
}
}
}
}
}
If anyone can elucidate me in this matter I would really appreciate it!
Cheers,
Popuppirate
Answer by Kiwasi · Nov 03, 2014 at 06:29 PM
The typical way woul be to loop through the list with for or for each. Create your GUI.Button inside the loop.
Edit: Code added
Just a couple of changes to your code, as commented
bool showmenu = false; // Moved outside the method to class scope
void OnGUI(){
if (GUI_use) {
GUI.Box (new Rect (10, 10, 100, 90), "Drone Control");
Player_Stats playerstats = this.transform.GetComponent<Player_Stats> ();
if (GUI.Button (new Rect (15, 15, 100, 50), "Build")) {
BuildDrone ();
}
if (GUI.Button (new Rect (15, 100, 100, 50), "Current Drones")) {
showmenu = true; //or showmenu = !showmenu;
} // Moved bracket
if (showmenu) {
for (int i=0; i<drone_type.Length; i++) {
if (GUI.Button (new Rect (15, 15 * i, 100, 50), drone_type [i])) {
// Do stuff
}
}
}
}
}
Edit: Here is a link to my YouTube video showing how to do this in the Unity 4.6 UI
Cheers Bored$$anonymous$$ormon, but it still doesn't work. Here is my new code:
private void OnGUI(){
if (GUI_use) {
GUI.Box (new Rect (10, 10, 100, 90), "Drone Control");
Player_Stats playerstats = this.transform.GetComponent<Player_Stats> ();
if (GUI.Button (new Rect (15, 15, 100, 50), "Build")) {
BuildDrone ();
}
if (GUI.Button (new Rect (15, 60, 100, 50), "Current Drones")) {
int count = 0;
foreach (string type in drone_type) {
count++;
if (GUI.Button (new Rect (15, 15 * count, 100, 50), type)) {
}
}
}
}
}
Sorry, just reread your code. Your problem is nothing to do with your loop. Your problem is the if statement it's inside.
GUI.Button will only return true once (on mouse down). To make your menu appear when the button is clicked set a bool inside the if. Then use that bool to deter$$anonymous$$e if your loop should run.
Sorry if I sound stupid, but Which if statement are you referring to? I tried the following and it didn't work because I assume I interpreted you wrong;
private void OnGUI(){
if (GUI_use) {
GUI.Box (new Rect (10, 10, 100, 90), "Drone Control");
Player_Stats playerstats = this.transform.GetComponent<Player_Stats> ();
if (GUI.Button (new Rect (15, 15, 100, 50), "Build")) {
BuildDrone ();
}
if (GUI.Button (new Rect (15, 60, 100, 50), "Current Drones")) {
bool clicked=false;
int count = 0;
foreach (string type in drone_type) {
clicked=true;
if (clicked){
count++;
if (GUI.Button (new Rect (15, 15 * count, 100, 50), type)) {
}
} else {
clicked=false;
}
}
}
}
}
Pseudo code:
bool show$$anonymous$$enu = false;
void OnGUI (){
if(GUI.Button(...)){
show$$anonymous$$enu = true;
}
if(show$$anonymous$$enu){
// Put your for loop here
}
}
Let me know if you still have trouble figuring out what I mean and I will try to find time to write out real code.
Cheers for all your help, but still no cigar! I really appreciate your time, so if you would I would be very appreciative!.
void OnGUI(){
bool showmenu = false;
if (GUI_use) {
GUI.Box (new Rect (10, 10, 100, 90), "Drone Control");
Player_Stats playerstats = this.transform.GetComponent<Player_Stats> ();
if (GUI.Button (new Rect (15, 15, 100, 50), "Build")) {
BuildDrone ();
}
if (GUI.Button (new Rect (15, 100, 100, 50), "Current Drones")) {
showmenu = true;
if (showmenu) {
for (int i=0; i<drone_type.Length; i++) {
if (GUI.Button (new Rect (15, 15 * i, 100, 50), drone_type [i])) {
}
}
}
}
}
}
Your answer
Follow this Question
Related Questions
changing GUI Button text with a string array 2 Answers
Unity Hangs/Crash when remove GUI Button. 2 Answers
Create GUI based on an array 2 Answers
Click on a button that is created post start. 0 Answers
A few GUI related questions. 1 Answer