- Home /
GUI Repeat Button problem
I have a problem with the following piece of code:
if(GUILayout.RepeatButton("Button"))
{
print("YES");
}
else
{
print("NO");
}
When i keep pressing this button i get YES NO YES NO YES NO in the console, which is what i dont want to happen. How can I get it to ignore the print("NO") bit while I keep pressing down this button?
Answer by Bunny83 · Oct 24, 2011 at 01:33 AM
Well, your problem is that OnGUI is executed several times a frame (at least two times: the Layout step and the Repaint step). Usually the input is processed in the Repaint event so you have to check for the Repaint event in your else statement.
if(GUILayout.RepeatButton("Button"))
{
print("YES");
}
else if (Event.current.type == EventType.Repaint)
{
print("NO");
}
ps. DON'T put the whole button into such an Event check, that would break the GUI system. The RepeatButton function (and all other GUI functions) have to be called for all Events.
You just saved me hours of debugging... thank you, sir/madam!
Your answer
Follow this Question
Related Questions
How to put a GUILayout button BESIDE another one instead of below? 1 Answer
GUI Button Text in upper left? 2 Answers
Unselect GUI Button 0 Answers
GUI Button for android? 1 Answer
GUISkin or otherwise - Once clicked, button stays same color 1 Answer