IEnumerator WaitForSeconds NullReference

Hi! I have this code bellow for when my player gets on a NPC collider. This NPC turns into child of a dialogue system gameobject, and then in the middle of the code this IEnumerator gets executed to type the sentences characters one by one, but when i leave the trigger and the NPC is not a child anymore, the dialogue panel gets disabled and it gives me a Nullreference. Is it because of the WaitForSeconds?


The code works perflectly, but i would like to know if there’s some way of not showing this error. Just for console porpuses.


IEnumerator TypeSentence(string sentence) {
    dialogueText.text = "";
    foreach (char letter in sentence.ToCharArray()) {
      dialogueText.text += letter;
      yield return new WaitForSeconds(.03f);
    }
  }

the dialogue panel gets disabled

Are you sure you just disable the panel and not Destroying it? If you get a NullReferenceException you usually get a detailed stacktrace and an exact line number. That’s all information you should have but you haven’t shown.

And to answer you guess: No, the WaitForSeconds object you create there will never caues a null reference exception. I would assume that “dialogueText” is destroyed. However since we have no idea how your whole setup looks like and where / when / how that coroutine is started we can’t really say anything about your issue.