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 AozakiKyuuji · Sep 18, 2013 at 07:38 AM · c#buttonmultiplebool

How do I disable one button out of multiple buttons without spamming declaration?

Hi,

I need some efficient coding help for my problem. My script goes like this,

 void OnGUI ()
 {
     if (GUI.Button(0, 0.....))
     playerSpeed += 10;
 
     if (GUI.Button(0, 50,.....))
     bulletSpeed += 10;
 }

That may seem simple enough. Now imagine copy pasting that GUI.Button for about 50 times.

My question is, I want to make sure that the same button is unclickable if it has been clicked before. The most no-brainer way, is the most troublesome way, where I declare 50 bools, one for each, so it does nothing when clicked. But I'm sure there is a much efficient way of doing this.

Any ideas to help?

Thanks in advance!

Comment
Add comment · Show 12
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 Benproductions1 · Sep 18, 2013 at 07:38 AM 0
Share

Use a collection

avatar image AozakiKyuuji · Sep 18, 2013 at 08:38 AM 0
Share

sorry, what is a collection? array?

avatar image Benproductions1 · Sep 18, 2013 at 08:45 AM 0
Share

An array is a type of collection. Not all collections are arrays

found on google

avatar image AozakiKyuuji · Sep 18, 2013 at 09:05 AM 0
Share

thanks! Though it solved another problem that I was pondering about, still I don't see how it will solve a bool problem.

Sorry, but may I ask if you could help with a short example?

avatar image Benproductions1 · Sep 18, 2013 at 09:08 AM 0
Share

it has a list of bools, all of which default to true, each of which represents one button. If a button is pressed it set's its relevant list value to false. The button is only rendered if it's list value is true. Quite simple, although GUI.enabled = true should get added on the bottom to make the code safe :)

Show more comments

1 Reply

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

Answer by syclamoth · Sep 18, 2013 at 08:51 AM

How paramaterised is 'DoSomething'? This seems like the kind of thing that could be simplified into a single for-loop! In any case, an array of booleans is the way to go-

 bool[] buttonsPressed = new bool[50];

Afterwards:

 for(int i = 0; i < buttonsPressed.Length; ++i) {
     GUI.enabled = !buttonsPressed[i];
     if(GUI.Button(/* Paramaters in terms of i! */)) {
         DoSomethingInTermsOfI(i);
         buttonsPressed[i] = true;
     }
 }
 GUI.enabled = true; // Edited per @Benproductions1's suggestion!
Comment
Add comment · Show 4 · 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 AozakiKyuuji · Sep 18, 2013 at 09:25 AM 0
Share

i've updated my question. Thanks, i'll give this a try and see what happens!

avatar image AozakiKyuuji · Sep 18, 2013 at 10:05 AM 0
Share

@syclamoth I'm sorry! Total noob here... While I know what your example means, I don't know how to add another GUI.Button to it...

How do I alter that if I have

 if (GUI.Button(0, 0.....))
     playerSpeed += 10;
  
 if (GUI.Button(0, 50,.....))
     bulletSpeed += 10;
avatar image syclamoth · Sep 18, 2013 at 10:18 AM 0
Share

Well, the for loop already makes 50 buttons! You need to modify the 'DoSomethingInTermsOfI' functions to act differently for different indicies. Try using a suite of functions like this:

 bool ParamaterisedButton(int i) {
     string text;
     Vector2 positionOffset;
     // this is an example, your exact offsets will probably be different
     positionOffset = new Vector2((i % 10) * 30, (i / 10) * 30);

     switch (i) {
         case 0:
             text = "player speed!"
             break;
         case 1:
             text = "bullet speed!"
             break;
        //... AND SO ON! $$anonymous$$ake 50 or so of these.
     }
     return GUI.Button(new Rect(positionOffset.x, positionOffset.y, 30, 30), text);
 }

Then, in 'DoSomething':

 switch(i) {
     case 0:
         playerSpeed += 10;
         break;
     case 1:
         bulletSpeed += 10;
         break;
 }
 // As before, make 50 of them!

Now, in your for-loop, ins$$anonymous$$d of calling GUI.Button directly, call

  if(ParamaterisedButton(i)) {
      DoSomething(i);
  }
avatar image AozakiKyuuji · Sep 18, 2013 at 10:25 AM 0
Share

Oh Right! switch cases! I need to get used to these and arrays to be more of a pro.

Thanks man! You rock!

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

17 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

Related Questions

Adding single Event listener to multiple ui buttons via scripting 1 Answer

Multiple Button Modifications 2 Answers

C# Why Won't My GUI Layout Button Appear? 1 Answer

Unity GUI multiple grids? 0 Answers

Multiple Cars not working 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