Do action after coroutine finishes

Hello, I’m pretty new to Unity and I’m trying to do something with coroutines.

I’m doing like a dialog game. First character speaks, then answers are shown and when I click, 2nd character speaks.

So let me explain my problem : i display text letter by letter (that’s why I use coroutines). But when I’m still displaying text of my 2nd character, the text of the 1st character starts displaying aswell ( his next text).

How can I do to make first coroutine starting after 2nd coroutine finished his job ?

I have tried with Action, but not working…

	IEnumerator ShowText()
	{
		currentText="";

		for(int i = 0; i <= fullTextActor.Length; i++){
			if(finish)
			{
				currentText=fullTextActor;
				GO.GetComponent<Text>().text = currentText;

				finish=false;
				Invoke("displayAnswers",0.1f);

				yield break;
			}
			currentText = fullTextActor.Substring(0,i);
			finish=false;

			GO.GetComponent<Text>().text = currentText;
			yield return new WaitForSeconds(delay);
			
		}
		 Invoke("displayAnswers",0.1f);
	}

	IEnumerator ShowTextClaude(Action doLast)
	{
		currentText="";

		for(int i = 0; i <= fullTextClaude.Length; i++){
			if(finish)
			{
				currentText=fullTextClaude;
				GOClaude.GetComponent<Text>().text = currentText;

				finish=false;
				//Invoke("displayAnswers",0.1f);

				yield break;
			}
			currentText = fullTextClaude.Substring(0,i);
			finish=false;

			GOClaude.GetComponent<Text>().text = currentText;
			yield return new WaitForSeconds(delay);
			doLast();
			
		}
	}	
       public void doLast()
	{
		DoneClaude = true;
	}

a small example could be something like

Edit: added custom functions for buttons


bool isFirstTalking, isSecondTalking;

    public void Start()
    {
        StartFirstTalk();
    }

    //call from button
    public void StartFirstTalk()
    {
        StartCoroutine(FirstTalk());
    }

    //call from button
    public void StartSecondTalk()
    {
        if(isSecondTalking) return;
        //wait 1st talk to finshed and start after
        if (isFirstTalking) { isSecondTalking = true; }
        //else start 2nd talk immediately
        else { StartCoroutine(SecondTalk()); }
    }

    //Auto dialogue
    //IEnumerator StartDialogue()
    //{
    //    yield return StartCoroutine(FirstTalk());
    //    //optional delay
    //    yield return new WaitForSeconds(1f);
    //    yield return StartCoroutine(SecondTalk());
    //}

    IEnumerator FirstTalk()
    {
        Debug.Log("First Talk Started");
        isFirstTalking = true;
        //exapmle logic
        for (int i=0; i<10f; i++)
        {
            Debug.Log("Letter " + i);
            yield return new WaitForSeconds(0.2f);
        }
        Debug.Log("First Talk Finished");
        isFirstTalking = false;

        if (isSecondTalking)
        {
            StartCoroutine(SecondTalk());
        }

        yield break;
    }

    IEnumerator SecondTalk()
    {
        Debug.Log("Second Talk Started");
        isSecondTalking = true;
        //exapmle logic
        for (int i = 0; i < 10f; i++)
        {
            Debug.Log("Letter " + i);
            yield return new WaitForSeconds(0.2f);
        }
        Debug.Log("Second Talk Finished");
        isSecondTalking = false;
        yield break;
    }

You may want to have a look at my WaitForUIButtons which I created for this question over here. It’s essentially just a small custom yield instruction that will register itself to the onclick event of the specified button and will resume the coroutine when that click happened. You can wait for multiple different buttons at once to provide a choice for the user. It also provides you the actual button that was clicked when it resumes. You can also specify a callback when a button has been pressed. So this class can also be used to easily attach a one-time event to a button that removes itself once executed.

I would highly recommend to not use too many coroutines / nested coroutines. It’s generally better to have one coroutine and maybe another nested level if really necessary to handle such sequential logic.