- Home /
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!
An array is a type of collection. Not all collections are arrays
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?
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 :)
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!
i've updated my question. Thanks, i'll give this a try and see what happens!
@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;
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);
}
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
Follow this Question
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