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

How do you pop up a window asking for input, then use that input?

Discussion in 'Getting Started' started by mrgreaper, Aug 27, 2017.

  1. mrgreaper

    mrgreaper

    Joined:
    Aug 6, 2017
    Posts:
    18
    Ok my google skills have let me down and i am completly stuck,

    heres what i am trying to do:

    User ticks a box to say they want multiple results
    They click the generator button and a popup dialog appears on the screen asking for an int
    they tap it in, hit an ok button
    the app takes the number they entered and generates that number of results.

    Must of that i have done i have an int called "times" and a toggle switch setup, forcing times to 10 and toggling multiple gives me 10 results (YAY)

    But heres where i am stuck, i can not for the life of me find out how i call up a popup dialog with an input for int's and an ok button with it, or how i would get the result. closest guess is its to do with gui.window, but that seems only for a button and looks to be very complex for such a simple task.

    any advice is greatfully receved
     
  2. mrgreaper

    mrgreaper

    Joined:
    Aug 6, 2017
    Posts:
    18
    I tried implementing the example code (if i can understand that then i may be able to adapt it)

    so inside my generate function and where i set the times to 10, and where i would need to call the window i added :
    if (multiRoll)
    {
    windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "My Window");
    times = 10;
    }

    I added the DoMyWindow function exactly as the tutorial page

    I added the windowRect public decoration

    i ran my code, first with out multiple rols on...no issues
    then with multiple rolls on ....crash, "windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "My Window");" can only be loaded from in OnGUI but then it would display instantly...not when i need it .....so i think i may be barking up the wrong tree?
     
  3. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Add an image to your UI for the dialog window. Make editfields, buttons and other UI object you need children of your dialog image. Set dialog image to inactive to hide. Within your script add a field for your dialog and when you want it to appear just set it to active. Set it back to inactive on your 'Ok' button click.
     
    JoeStrout likes this.
  4. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Your using the old GUI system. See the tutorials on the current system here.
     
  5. mrgreaper

    mrgreaper

    Joined:
    Aug 6, 2017
    Posts:
    18
    This is for a simple pop up input box though not for the main ui (already have a main ui) seems a bit weird to create a whole new ui for one popup box ?

    This seems a terrible way of doing things but im adding it as an element to my main ui and will just disable it as needed
     
    Last edited: Aug 27, 2017
  6. mrgreaper

    mrgreaper

    Joined:
    Aug 6, 2017
    Posts:
    18
    Ok so heres the solution.

    I had to re do my generate function, will explain further down,
    i then went to my ui and added an image
    then added a input field
    then added a button
    then added a label

    This created the "How many box"
    which i then disabled

    in my script i added:

    public GameObject howManyPopUp;
    private InputField HowManyIn;

    then dragged my new "image" into the new slot in my script

    in void start i added
    HowManyIn = howManyPopUp.GetComponent<InputField>();

    Then when a player hits generate my program checks to see if multi generate is set to true, if it is not then it calls my generate function and passes the times value of 1 (ie generate once)
    If however it is true then it simply enables the How Many dialog window with :
    howManyPopUp.SetActive(true);
    and then it sits there all smug, its job is done after all

    the player sees the window and assumes the code is still running, and enters a numeric value of how many times (limited to 3 characters so they can not go crazy!)

    and when they hit the "ok button" this code is run :
    public void multiGenerate()
    {
    int times = 1;
    string Generator = spinner.options[spinner.value].text;
    try
    {
    times = int.Parse(HowManyIn.text);
    }
    catch (System.Exception)
    {

    times = 10;
    }
    doGenerate(Generator, times);
    howManyPopUp.SetActive(false);
    }
    as you can see i check to see if they have entered non numbers in the box and fix it if they have.

    Most assuredly the worst way i have ever seen to implement popup windows in coding but im guessing there is an easier way that i just do not know about (or i did it wrong) but hey the code works! horses mouth, not looking, etc etc

    (hopefully the solution i used will help the next person googling the issue, though it should be noted i have no idea if this is the "right" way)



    **EDIT**
    Actually its not working, its always generating 10 so something is going wrong with parsing the int from the Input Field...rats!
     
  7. mrgreaper

    mrgreaper

    Joined:
    Aug 6, 2017
    Posts:
    18
    Taken an hour and a half of "Object referenced is not an instance of object" but fixed it (though the original implementation really should of been ok.
    I made HowManyIn a public reference and dragged the input field into it, seems getting it from the gameobject does not work!

    Oh well working now and if some wone else has the same issue i just saved them an hour and half of frustrating table headbutting lol
     
  8. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Glad you were able to work it out.

    In the future please use code tags when posting code. See how here.

    It's a good idea to have few preconceived notions about Unity and how it works when starting out. The information found in the tutorials can be a great help. Being new here, it would be a good idea to spend some quality time with them, just to see how Unity was designed to be used.

    Get use to table headbutting, that feature was added to Unity in version 1.0 ;-)