- Home /
Debug.Log - Pressing Button readings.
Hello everyone.
I need desperate help on what's going on, I've been here for like an hour looking at code and still have no idea what's wrong.
Before I ask my question, here's the code of a not-so-simple(due to functionality) button layout.
void OnGUI () {
CreateDPAD();
SetIdleness();
}
Create DPAD states the obvious. SetIdleness checks if any button is being pressed.
private void CreateDPAD(){
CreateRightButton();
CreateLeftButton();
CreateUpButton();
CreateDownButton();
}
private void CreateUpButton(){
_upButton = UpButtonClicked ();
if (_upButton)
{
//Animation code, irrelevant to the problem. :D
}
}
private bool UpButtonClicked(){
//upClicked = true;
if(GUI.RepeatButton(new Rect(Screen.width - (BUTTON_WIDTH * 2) - 10,Screen.height - (BUTTON_HEIGHT * 3),50,50),"", "Dpad_ArrowU")){
UpActive = true;
return true;
} else {
UpActive = false;
return false;
}
}
Here's where my debug statements are.
private void SetIdleness(){
//Debug.Log ("SettingIdleness");
if(DownActive || UpActive || LeftActive || RightActive){
Debug.Log ("A Button is Active!");
} else if(!DownActive && !UpActive && !LeftActive && !RightActive){
Debug.Log ("No Button Active");
}
}
Now, here's the wierd part.
This function checks if Im pressing any buttons. If no button is pressed Debug("No active Buttons") else ("Active Button"); On my dbg WHILE any button is pressed I get this:
1st Line: Active Button..
2nd Line: No activeButtons...
Why is this happening???
Thanks for your time and help, I've been looking at code for an hour and I don't know whats going on....
OnGUI is often called multiple times per frame for various reasons. This could be related.
@Bored$$anonymous$$ormon
Even though the button was never let go?(Unclicked..)
Yes. There are always multiple passes.
Where exactly are you putting your debug.log statements?
@Bored$$anonymous$$ormon
The log statements are inside SetIdleness function, inside the OnGUI, AFTER Create DPAD.( Just like in the code I provided..)
@Bored$$anonymous$$ormon
I even added UpActive because _upButton, which is basically a boolean that equals to what the button returns(if is pressed true, if not, false)...I added UpActive to try and see if that was the problem..It wasnt :(
Answer by drodrii · Jul 22, 2014 at 06:39 PM
The problem is that it gets executed several times. You can check it e.g. by also showing Event.current, which will tell you that first the layout is handled and then the actual repainting takes place. Only during the repainting, you are going to get the correct answer. If you show the value e.g. in Update or any other place than OnGUI, you are going to get the correct result. The problem is that you are using and else for the GUI.RepeatButton. This does not work with an immediate gui solution. This answer was given by @Dantus and it is the solution.
Answer by perrygarg · Jul 22, 2014 at 06:23 AM
According to me in your console window, 'Collapse' button is pressed, due to which same kind of messages are being grouped together as i show in example image above(highlighted with yellow highlighter)..just disable it and try again. I think your problem should b solved by this. Gud luck :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613154653im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making texture cover whole button 1 Answer
GUI.DrawTexture on GUI.Button press 1 Answer
[FIXED] Why is my GUI.Button always pressed? 0 Answers
iPhone Touch Screen? 2 Answers