Hi everyone ! I made a dialogue system in my narrative game for school using Brackeys tutorial (
). All is working well, except I just don’t know how to go from one NPC to another…
He says at some point to create a second game object with the dialogue script attached to it. But how to I tell the game to change to the second NPC’s dialogue once the first one is over (so that the NPC name changes for example) ? And then to go back to the first NPC ? (basically to make them have a conversation) I’m still very new to Unity and I really don’t know how to pull this off… My guess is that in the if(sentences.COunt == 0) under the DisplayNextSentence method I should tell Unity to play the next NPC instead of that useless EndDialogue() method (there used to be just a print “End Dialogue” in this method but I took it off) but I don’t really know how to play this…
this is what the dialogueTrigger code looks like :
public class DialogueManager : MonoBehaviour {
public Text nameText;
public Text dialogueText;
private Queue<string> sentences;
void Start () {
sentences = new Queue<string>();
}
public void StartDialogue(Dialogue dialogue)
{
nameText.text = dialogue.name;
sentences.Clear();
foreach(string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if (sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
StopAllCoroutines();
StartCoroutine(TypeSentence(sentence));
}
//Typewriter effect
IEnumerator TypeSentence(string sentence)
{
dialogueText.text = "";
foreach (char letter in sentence.ToCharArray())
{
dialogueText.text += letter;
yield return null;
}
}
void EndDialogue()
{
}
void Update()
{
//Continue with return and space key
if(Input.GetKeyDown(KeyCode.Return))
{
DisplayNextSentence();
}
if (Input.GetKeyDown(KeyCode.Space))
{
DisplayNextSentence();
}
}
}
thank you very much for your help !