WaitForSeconds wont work

Hey there, I have a strange problem with my code.
As you can see below this code should display a certain text with a typewriter effect.

First text01 should be printed (text is assigned in unity interface) and after 10.5 seconds the text should get removed and text02 should be printed.

However the WaitForSeconds doesn’t seem to work.
While text01 gets printed, text02 gehts printed too … this is also true for text03 …

Whats the matter?
Hope you can help me :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TypeWriterEffect : MonoBehaviour {

    public float delay = 0.1F;
    public string text01;
    public string text02;
    public string text03;
    private string currentText = "";

 
	// Use this for initialization
	void Start () {
        StartCoroutine(writeText(text01));

        clearText();

        StartCoroutine(writeText(text02));

        clearText();

        StartCoroutine(writeText(text03));
    }


    void clearText()
    {       
        this.GetComponent<Text>().text = "";        
    }

    IEnumerator writeText(string text){
        for (int i = 0; i < text.Length; i++)
        {
            currentText = text.Substring(0, i + 1);
            this.GetComponent<Text>().text = currentText;

            yield return new WaitForSeconds(delay);
        }
        yield return new WaitForSeconds(10.5F);
    }

}

I dream that someday, people will understand that WaitForSeconds does not pauses the execution of the whole script or the function which calls the Coroutine, but only the coroutine itself

void Start()
{
    string[] texts = {text01, text02, text03};
    StartCoroutine( texts ) ;
}

IEnumerator writeText(string[] texts)
{
    WaitForSeconds letterDelay = new WaitForSeconds(delay) ;
    WaitForSeconds textDelay = new WaitForSeconds(10.5f) ;
    Text textComponent = GetComponent<Text>() ;
    
    for( int textIndex = 0 ; textIndex < texts.Length ; ++textIndex )
    {
        string text = texts[textIndex] ;
        for (int letterIndex = 0; letterIndex < text.Length; ++letterIndex)
        {
            yield return letterDelay;
            
            currentText = text.Substring(0, letterIndex + 1);
            textComponent.text = currentText;

        }
        yield return textDelay;
        textComponent.text = "" ;
    }
}

An alternative is to declare the Start function as a coroutine and do as follow :

 IEnumerator Start () {
     yield return StartCoroutine(writeText(text01));

     clearText();

     yield return StartCoroutine(writeText(text02));

     clearText();

     yield return StartCoroutine(writeText(text03));
 }


 void clearText()
 {       
     this.GetComponent<Text>().text = "";        
 }

 IEnumerator writeText(string text){
     for (int i = 0; i < text.Length; i++)
     {
         currentText = text.Substring(0, i + 1);
         this.GetComponent<Text>().text = currentText;

         yield return new WaitForSeconds(delay);
     }
     yield return new WaitForSeconds(10.5F);
 }