Search Unity

GUI Button problem

Discussion in 'Immediate Mode GUI (IMGUI)' started by gk104, Aug 1, 2014.

  1. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    Code (CSharp):
    1.  
    2.     void OnGUI(){
    3.  
    4.     if(TimerScript.TimerCount > 0f)
    5.     {
    6.             GUI.backgroundColor = new Color(0,0,0,0);
    7.             BackToMain = GUI.Button (new Rect (Width,Height,WidthSize,HeightSize), PauseButtonGUI);
    8.  
    9.            
    10.             if(BackToMain)
    11.             {
    12.                 Debug.Log("TRUE");
    13.             }
    14.             else
    15.             {
    16.                 Debug.Log("FALSE");
    17.             }
    18.     }
    Why when i press the button, it keeps being true for one frame? it shouldn't be true until i press again to make it false?
     
  2. haklil

    haklil

    Joined:
    Aug 5, 2014
    Posts:
    1
    If im not totally wrong, buttons only return true when they are pressed, so if you want it to stay true until you press it again, you should add a bool var. Your if-statement could be like this:

    bool boolvar ;

    if(BackToMain) && boolvar = true {
    boolvar == false ;
    Debug.Log("FALSE");
    }
    else if(BackToMain) && boolvar = false {
    boolvar == true ;
    Debug.Log("TRUE");
    }

    Sorry if it is a bit wrong, I have not programmed for a while. But i hope you get the idea.