Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

AutoType text, Coroutines, and how to not to use them.

Discussion in 'Scripting' started by Micio_del_Cheshire, Sep 14, 2018.

  1. Micio_del_Cheshire


    Oct 24, 2013
    Hello there.
    I was trying to get a simple text to autotype its content (typewriter style).
    I'm parsing the text content, some of it has to remain written, some of it has to be removed after being written.

    This is the code about autotyping.

    Code (CSharp):
    1.  public void AutoType(string s)
    2.     {
    3.         if (!typing)
    4.         {
    5.             stringToPrint = s;
    7.             StartCoroutine(Type(s));
    8.         }
    9.         else
    10.         {
    11.             Debug.LogError("is typing");
    12.         }
    13.     }
    15.     private IEnumerator Type(string s)
    16.     {
    17.        typing = true;
    18.        foreach (char c in s.ToCharArray())
    19.        {
    20.             text.text += c;
    21.             yield return new WaitForSeconds(typeSpeed);
    22.        }
    23.         Debug.Log("FINISHED");
    24.        typing = false;
    25.     }
    The question is: I'm using Coroutines, ad I don't want to. Coroutines allow the program flow to continue. In this way I'm writing a temporary text context, and in the meantime it's been autotyped, the game is already parsing and processing the next text, messing with the removal process afterwards.

    I'm ok with the game to wait, but I can't make the game wait unless using coroutines.
    - how can I autotype a text, being able to know when it finishes typing? I tried something like
    Code (CSharp):
    1. autotext.AutoType(string);
    2. while(autotext.IsTyping);
    but it's going in endless loop.

    Thanks for the help
  2. GroZZleR


    Feb 1, 2015
    So the issue is that you have more than one phrase to typewriter text and the program is getting ahead of itself? You can have one coroutine wait for the other coroutine to finish before continuing to overcome that:

    Code (csharp):
    2. IEnumerator BeginSpeech(List<string> phrases)
    3. {
    4.    for(int i = 0; i < phrases.Count; ++i)
    5.    {
    6.        yield return StartCoroutine(Type(phrases[i]));
    7.    }
    8. }
    That will wait for Type to finish before moving on to the next phrase.
  3. Micio_del_Cheshire


    Oct 24, 2013
    Thanks, I will try it :)
  4. AndersMalmgren


    Aug 31, 2014
    You dont need todo StartCoroutine

    Code (CSharp):
    1. private IEnumerator BeginSpeech(List<string> phrases)
    2. {
    3.    foreach(var phrase in phrases)
    4.       yield return Type(phrase);
    5. }