- Home /
Buttons that remove themselves when clicked mess up other gui elements
Basically, I'm trying to make buttons within a panel which, when clicked, create an object which can be dragged and dropped into the world. The button should remove itself from the panel when clicked. Doing this has caused some pretty strange behavior, though:
if (!pickUpFlag && GUI.RepeatButton(new Rect(70,25, 50, 50), "Magic"))
{
pickUpFlag = true;
}
if (!endFlag && GUI.RepeatButton(new Rect(10,85, 50, 50), "End"))
{
endFlag = true;
}
if (!startFlag && GUI.RepeatButton(new Rect(10,25, 50, 50), "Start"))
{
startFlag = true;
}
The above code functions weirdly... clicking either of the first two buttons works fine, however, clicking the last button IN CODE (the start button) causes some issues; Buttons will no longer highlight, and in some circumstances (that I can't replicate) the gui will show one of the other buttons as active/clicked when it is moused over. Additionally, anything in other parts of the code which is supposed to execute on !Input.GetMouseButton(0) won't execute until the mouse button is clicked again. Weird stuff. Any ideas?
NOTE: I am using RepeatButton because you're supposed to drag the item off of the button and release it into the world... a regular Button doesn't return true until I release the mouse button (while mouse is stull on top of it), but RepeatButton returns true as soon as it is pressed. The way it works in my program is: The user presses the mouse button over one of these buttons. When the mouse button is depressed, the button disappears and a graphic/model appears attached to the mouse. That can be dragged and dropped into the world. Regular buttons won't work because they don't return true until I release the mousebutton.
Excuse me, but why do you use RepeatButton? Have you tried to put two nested if conditions ins$$anonymous$$d of joining the two conditions in one if?
Answer by DaveA · Mar 17, 2011 at 05:11 PM
Do these need to be RepeatButtons? Those are for 'continuous events' like holding a key down. But your code implies that these are single-use, as clicking one will turn it off. I assume all those vars are set to false initially. Try using just Button instead, see if that helps.
using repeatbutton because you're supposed to drag the item off of the button and release it into the world... a regular button doesn't return true until I release the mouse button, but RepeatButton returns true as soon as it is pressed. I will update the question.
In which case I'd try using Update to do the drag. Get mouse position, check against the rect of the button, move to follow mouse.
Answer by MithosAnnar · Feb 12, 2012 at 01:19 PM
If you want to make the button disappear use this script:
var bool : boolean = false;
if ( bool == false) {
if ( GUI.Button( Rect(70, 25, 50, 50), "Something")) {
bool = true;
}
}
Your answer
Follow this Question
Related Questions
Change button texture when its clicked 1 Answer
Why can't i click a button when in first person? 2 Answers
GUI based character control 2 Answers
Can I change GUIButton behaviour? 1 Answer
Move the text in a GUI Button/Box 1 Answer