Search Unity

Pop up message, pause script till user clicks OK button

Discussion in 'Scripting' started by IngeJones, May 24, 2015.

  1. IngeJones

    IngeJones

    Joined:
    Dec 11, 2013
    Posts:
    129
    Basically:
    Code (csharp):
    1.  
    2.     public void SomeLoop ()
    3.     {
    4.         List<GameObject> gogos = new List<GameObject>();
    5.         // add some gameobjects
    6.  
    7.         foreach (GameObject go in gogos)
    8.         {
    9.             PopUpMessageAboutGameObject(go);
    10.             DoSomeMoreStuff(go); // but not until the user clicked the OK button see below!
    11.         }
    12.     }
    13.  
    14.     public void PopUpMessageAboutGameObject (GameObject go)
    15.     {
    16.         // Show my message panel complete with an OK button
    17.         // and DO NOT return from here until user clicks OK
    18.     }
    19.  
    20.     void DoSomeMoreStuff(GameObject go)
    21.     {
    22.     }
    23.  
    24.  
    I have read lots of articles about threads and coroutines and sleep and timescales but I can't get my head round how they'd fit here
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    I simply would add a bool variable to your script. Set it to false on PopUpMessageAboutGameObject and set it to true if the user clicks ok. Then use
    Code (CSharp):
    1. if(myFlag == true){
    2. DoSomeMoreStuff(go);
    3. }
     
    IngeJones likes this.
  3. IngeJones

    IngeJones

    Joined:
    Dec 11, 2013
    Posts:
    129
    Thanks Duugu. After several hours I finally worked out how to make a coroutine work for what I wanted, but it was very torturous lol. I am afraid your solution doesn't look like it would have worked for what I needed, as I do want all the SomeMoreStuff to happen every time, just not before I have read the message, and a normal while loop busied up the processor so much it couldn't take my input :)
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    One possible solution in pseudo code.

    Code (CSharp):
    1. bool isButtonClicked;
    2.  
    3. IEnumerator WaitFunction () {
    4.     // Do stuff
    5.     // Show box
    6.     while (!isButtonClicked) yield return null;
    7.     // Do other stuff
    8. }
     
    IngeJones likes this.
  5. IngeJones

    IngeJones

    Joined:
    Dec 11, 2013
    Posts:
    129
    I did something similar, but I actually set a bool Paused when the message box pops up, and unset Paused when I click the button, and my coroutine checks for that.