Hi, I have a script to controll dialogues, but I found that for some reason it will skipt half of the sentences. For example, if I have 5 (0-4) sentences it will only show the sentences 0,2 and 4.
Does anybody know why this happends?
Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogueManager : MonoBehaviour
{
public TMP_Text nameText;
public TMP_Text dialogueText;
public Animator animator;
private Queue<string> sentences;
public float timeForSentece = 5f;
// Start is called before the first frame update
void Start()
{
sentences = new Queue<string>();
}
public void StartDialogue (Dialogue dialogue)
{
animator.SetBool("isOpen", true);
nameText.text = dialogue.personName;
sentences.Clear();
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
StartCoroutine(ChangeSentece(dialogue));
}
public void DisplayNextSentece()
{
if (sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
dialogueText.text = sentence;
}
IEnumerator ChangeSentece(Dialogue dialogue)
{
DisplayNextSentece();
foreach (string sentence in dialogue.sentences)
{
yield return new WaitForSeconds(timeForSentece);
DisplayNextSentece();
}
if (sentences.Count == 0)
{
yield return null;
}
}
public void EndDialogue()
{
animator.SetBool("isOpen", false);
}
}