Wait For User to click GUI.Button

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.

//PlayerTurn function
void  PlayerTurn(){
  isPlayerTurn = false;
  //Check if is first turn
  if (isFirstTurn == true) {
   //Draw cards
   FirstDraw();

   ShowRedrawButtons = true;

   //Wait here for user input
   //while (ShowRedrawButtons == true){
   //}

   isFirstTurn = false;
  }

  //PlayerHand.Add(DrawCard());

  for(int i = 0;i <= PlayerHand.Count - 1;i++){
   Debug.Log((string)PlayerHand[i]);
  }
}

and this is my OnGUI Function

void OnGUI(){
        if (ShowRedrawButtons == true) {
            if (GUI.Button (new Rect (Screen.width / 2 - 110, Screen.height / 2, 100, 50), "Redraw")) {
                Redraw ();
                ShowRedrawButtons = false;
            }
            if (GUI.Button (new Rect (Screen.width / 2 + 110, Screen.height / 2, 100, 50), "Don't Redraw")) {
                ShowRedrawButtons = false;
            }
        }
    }

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

That’s because OnGUI() never gets called because you are just looping forever in lines 12-13.

Replace it with:

if (ShowRedrawButtons == true)
   return;

This will just bypass everything in the update() until they click the button

1 Like

It’s not exactly the thing i need but i can tweak it to my needs … i hope :slight_smile:

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

yield wait for seconds use it