Everything is working fine.
In the DialogueManager I’m using the variable numberofsentences to get the number of sentences from the Dialogue class. If I’m using the sentences variable in the DialogueManager it’s showing only 2 sentences but the variable of sentences in the Dialogue class showing 3 sentences.
I tried before to loop over the sentences variable in the DialogueManager but the Count is 2. It’s showing only 2 sentences.
And I have in the editor 3 sentences with text inside. Why in the DialougeManager the variable sentences show only 2 and not 3 ?
Each TextArea in the Dialogue class is a sentence.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour
{
public DialogueTrigger dialogueTrigger;
public Text dialogueText;
public float sentencesSwitchDuration = 7f;
private Queue<string> sentences;
private int numberofsentences;
// Use this for initialization
void Start ()
{
sentences = new Queue<string>();
dialogueTrigger.TriggerDialogue();
StartCoroutine(DisplayNextSentenceWithDelay());
}
public void StartDialogue(Dialogue dialogue)
{
Debug.Log("Starting conversation with " + dialogue.name);
sentences.Clear();
numberofsentences = dialogue.sentences.Length;
foreach(string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if (sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
dialogueText.text = sentence;
}
public IEnumerator DisplayNextSentenceWithDelay()
{
for (int i = 0; i < numberofsentences + 1; i++)
{
yield return new WaitForSeconds(sentencesSwitchDuration);
DisplayNextSentence();
}
}
void EndDialogue()
{
Debug.Log("End of conversation.");
}
}
The trigger:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialogue()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
Dialogue:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialogue
{
public string name;
[TextArea(3, 10)]
public string[] sentences;
}