Search Unity

Wait For User to click GUI.Button

Discussion in 'Scripting' started by wisien92, Sep 9, 2014.

  1. wisien92

    wisien92

    Joined:
    Nov 16, 2012
    Posts:
    56
    Hi, i am writting simple card game just to learn programming and i have been wondering how to wait in application for user to click button.

    The idea is that i start the game and draw cards - but after that i want to give user choice to redraw cards- so i show gui buttons. But the problem is that the code continues to execute or when i make infinite loop unity freezes.

    Code (CSharp):
    1. //PlayerTurn function
    2. void  PlayerTurn(){
    3.   isPlayerTurn = false;
    4.   //Check if is first turn
    5.   if (isFirstTurn == true) {
    6.    //Draw cards
    7.    FirstDraw();
    8.  
    9.    ShowRedrawButtons = true;
    10.  
    11.    //Wait here for user input
    12.    //while (ShowRedrawButtons == true){
    13.    //}
    14.  
    15.    isFirstTurn = false;
    16.   }
    17.  
    18.   //PlayerHand.Add(DrawCard());
    19.  
    20.   for(int i = 0;i <= PlayerHand.Count - 1;i++){
    21.    Debug.Log((string)PlayerHand[i]);
    22.   }
    23. }
    and this is my OnGUI Function

    Code (CSharp):
    1. void OnGUI(){
    2.         if (ShowRedrawButtons == true) {
    3.             if (GUI.Button (new Rect (Screen.width / 2 - 110, Screen.height / 2, 100, 50), "Redraw")) {
    4.                 Redraw ();
    5.                 ShowRedrawButtons = false;
    6.             }
    7.             if (GUI.Button (new Rect (Screen.width / 2 + 110, Screen.height / 2, 100, 50), "Don't Redraw")) {
    8.                 ShowRedrawButtons = false;
    9.             }
    10.         }
    11.     }

    In line 12-13 if i uncommet the application freezes. I have no idea how to achieve this in other way. I call PlayerTurn in update function
     
  2. AndyLL

    AndyLL

    Joined:
    Aug 25, 2013
    Posts:
    75
    That's because OnGUI() never gets called because you are just looping forever in lines 12-13.

    Replace it with:

    Code (CSharp):
    1. if (ShowRedrawButtons == true)
    2.    return;
    This will just bypass everything in the update() until they click the button
     
    wisien92 likes this.
  3. wisien92

    wisien92

    Joined:
    Nov 16, 2012
    Posts:
    56
    It's not exactly the thing i need but i can tweak it to my needs ... i hope :)

    The problem now is that i need to start from the point we stopped but i think i''ll work it out.
     
  4. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    yield wait for seconds use it