WaitForSeconds in For loop

Why does my waitforseconds not work? the Talk() function is called by another object.

    public void Talk()
    {
        npcAi.enabled = false;
        animation.CrossFade("idle");

        dialogueText.enabled = true;

        if (textFile != null)
        {
            for (int i = 0; i < lines.Count; i++)
            {

                dialogueText.text = lines*;*

StartCoroutine(Wait());
Debug.Log(lines*);*
}

npcAi.enabled = true;
//dialogueText.enabled = false;
}

}

IEnumerator Wait()
{
yield return new WaitForSeconds(5.0f);
}

Hi

this is because you start the waiting in a coroutine which means it does no longer affect the “main execution”.

to simplyfy and since I do not know where you call the talk function you can do something like:

public void Talk()
{
   StartCoroutine(TalkAsync());
     
}
     
IEnumerator TalkAsync()
{
    npcAi.enabled = false;
    animation.CrossFade("idle");
     
    dialogueText.enabled = true;
     
    if (textFile != null)
    {
       for (int i = 0; i < lines.Count; i++)
       {
     
          dialogueText.text = lines*;*

yield return new WaitForSeconds(5.0f);
Debug.Log(lines*);*
}
npcAi.enabled = true;
//dialogueText.enabled = false;
}
}
hope it helps