Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by popuppirate · Nov 03, 2014 at 06:25 PM · guibuttonarray

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image popuppirate · Nov 03, 2014 at 06:36 PM 0
Share

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)) {
                                 
                                         }
                             
                                 }
                         }
                 }
         }
 

avatar image Kiwasi · Nov 03, 2014 at 06:43 PM 0
Share

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.

avatar image popuppirate · Nov 03, 2014 at 06:54 PM 0
Share

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;
                                     }
                             
                                 }
                         }
                     }
                 }
 
 
                                     

 
avatar image Kiwasi · Nov 03, 2014 at 07:17 PM 1
Share

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.

avatar image popuppirate · Nov 03, 2014 at 07:31 PM 0
Share

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])) {
                                             }
                             
                                     }
                         
                             }
                     }
             }
     }


                                 
 
                                 
 
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges