Search Unity

"Pausing" a script until receive a variable

Discussion in 'Editor & General Support' started by BlakeGillman, Feb 28, 2018.

  1. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    (I have done lots of looking up prior to posting, but no answers seem helpful to my case)

    I have a script that receives a string from a user input UI and then continue with what it was doing.

    For example,
    Debug.Log("What's your name?");
    string input = Console.WaitForInput ();
    Debug.Log("Nice to see you" + input);


    It's accessing Console from a variable, currently WaitForInput is a blank method - I'm not sure exactly what to do in order for it to wait to receive a string from one of the default unity UI InputField
     
    Last edited: Feb 28, 2018
  2. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Its not so easy to explaine without code but i made a example for you.
    Hope it helps to understand the parts required in building such a script.
    If you have questions about it just ask.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. // make sure to include the UnityEngine.UI namespace so we can access the Text and Inputfield.
    5. using UnityEngine.UI;
    6.  
    7. /// <summary>
    8. /// Story manager Class
    9. /// </summary>
    10. public class StoryManager : MonoBehaviour
    11. {
    12.  
    13.     public InputField UserInputField; // the textfield the user is typing, it needs to be assigned in the inspector
    14.     public Text TextOutput;         // the output text that we what to show the user, it needs to be assigned in the inspector
    15.     bool gotUserNameInput = false; // bool variable to save if we got some user input in the input field
    16.  
    17.     // Use this for initialization
    18.     void Start()
    19.     {
    20.         StartCoroutine(Story()); // starting the story coroutine , make sure the function is of type IEnumerator
    21.     }
    22.  
    23.  
    24.     /// <summary>
    25.     /// Our Story Coroutine.
    26.     /// </summary>
    27.     /// <returns></returns>
    28.     IEnumerator Story()
    29.     {
    30.         yield return new WaitForSeconds(1); // waiting one second to start our story
    31.         TextOutput.text = "What's your name?"; // set the text to the text display
    32.         UserInputField.gameObject.SetActive(true); // enable the input field object
    33.         UserInputField.onEndEdit.AddListener(delegate { TextEditEnded(UserInputField); }); // subscribe the TextEditEnded function to the onEndEdit event of the user input field.
    34.         while (!gotUserNameInput) // loop untill we have user input
    35.         {
    36.             yield return new WaitForSeconds(0);// waiting one frame alternatively you can use yield return null for waiting one frame
    37.         }
    38.  
    39.         TextOutput.text = "Nice to see you " + UserInputField.text; // set the answer text including the given name from the input field
    40.         UserInputField.gameObject.SetActive(false); // disable the input field since we do not have any use for it at the moment
    41.         gotUserNameInput = false; // reset gotUserNameInput in case we want to run it again.
    42.         yield return null; // obligatory return statement
    43.  
    44.     }
    45.  
    46.     /// <summary>
    47.     /// This function gets called when the user is ending his editing of the UserInputField,
    48.     /// this can happenif he presses enter to finis the input or focuses some other ui element.
    49.     /// </summary>
    50.     /// <param name="input"></param>
    51.     public void TextEditEnded(InputField input)
    52.     {
    53.         if(!string.IsNullOrEmpty(input.text)) // Check if the user has input someting
    54.             gotUserNameInput = true; //set gotUserNameInput to true to signal that we have got input so the story can continue
    55.     }
    56.  
    57.  
    58. }
    59.