Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GUI Script question

Discussion in 'Scripting' started by snortop, Mar 22, 2012.

  1. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    I made this GUI code to set some movement.
    I made it in a function.
    It show up on screen but the button wont show any debug or anything.

    it c#
    Code (csharp):
    1.  
    2.     public Rect WindowMOVEMENT = new Rect(Screen.width / 2 - (165 / 2), Screen.height / 2 - (51 / 2), 165, 51);
    3.  
    4.     void OnGUI() { 
    5.         WindowMOVEMENT = GUI.Window(2, WindowMOVEMENT, win_movement, "Move");
    6.     }
    7.  
    8.     private void win_movement(int windowID) {
    9.         GUI.DragWindow();
    10.         shipRotation = 0;
    11.         GUILayout.BeginHorizontal();
    12.         if (GUILayout.Button("Left")) {
    13.             Debug.Log("Setting Direction Left");
    14.             shipRotation = 1;
    15.         }
    16.         if(GUILayout.Button("Middle")) {
    17.             Debug.Log("Setting Direction Middle");
    18.             shipRotation = 0;
    19.         }
    20.         if(GUILayout.Button("Right")) {
    21.             Debug.Log("Setting Direction Right");
    22.             shipRotation = -1;
    23.         }
    24.         GUILayout.EndHorizontal();
    25.     }
    26.  
    But if i take out of the function and insert it like this in OnGUI it work.

    Code (csharp):
    1.  
    2.         GUILayout.BeginArea(WindowMOVEMENT, "Movement", GUI.skin.window);
    3.         GUILayout.BeginHorizontal();
    4.         if (GUILayout.Button("Left")) {
    5.             Debug.Log("Setting Direction Left");
    6.             shipRotation = 1;
    7.         }
    8.         if(GUILayout.Button("Middle")) {
    9.             Debug.Log("Setting Direction Middle");
    10.             shipRotation = 0;
    11.         }
    12.         if(GUILayout.Button("Right")) {
    13.             Debug.Log("Setting Direction Right");
    14.             shipRotation = -1;
    15.         }
    16.         GUILayout.EndHorizontal();
    17.         GUILayout.EndArea();
    18.  
    Can anyone explain this to me?
    What am i doing wrong?
     
    Last edited: Mar 22, 2012
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  3. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    im not using beginarea in the other code whit window and function .. only in the code that work just straight in the GUI area.
     
  4. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    If you want to put the buttons in a window, I believe this:

    Code (csharp):
    1.  
    2. void OnGUI() {  
    3.         GUILayout.BeginArea(WindowMOVEMENT, "Movement", GUI.skin.window);
    4.     }
    5.  
    Should be:

    Code (csharp):
    1.  
    2. void OnGUI() {  
    3.         GUI.Window(0, WindowMOVEMENT , win_movement, "Set Movement");
    4.     }
    5.  
     
  5. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    A function that contains GUI calls cannot be called from outside of OnGUI. I suspect that you call win_movement from Start or Awake which won't work. You need to call win_movement from OnGUI to make stuff like GUI.DragWindow work.

    This will not work :

    Code (csharp):
    1.  
    2. void Update ( ) {
    3.    win_movement(5);
    4. }
    5.  
    This will work :

    Code (csharp):
    1.  
    2. void OnGUI( ) {
    3.    win_movement(5);
    4. }
    5.  
     
  6. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    oooh i see now why you keep posting about this BeginArea

    I copied the wrong linie of code to the first code.. i corrected that now..
    I didnt think of checking that.. but i did use GUI.Window.. and i didnt have any problem whit the code.. other the fact that the button dont work.
    Sorry for not realizing that before now :D

    So my question is why do the button clicking not work in the fist code.
    but it work in the 2 code.
     
    Last edited: Mar 22, 2012
  7. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    What?? o_O

    no one doing that anyway!!