Dialogue Trigger Help

Updated on October 25, 2018 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on October 24, 2018

 I was doing the tutorial on dialogue trigger on Brackey’s and I came apon a error that I cannot solve. Please help. Thanks.

 

 

 

 

 

 

 

It tells me”Object reference not set to the instance of an object for sentences.”

 

 

 

The error line is sentences.Clear(); in the second script.

 

 

 

I trigger it here:

 

 

 

 

 

 

 

 
 
 
 
 
 
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 
 
 
 
 
 
public class GideonShopManager : MonoBehaviour {
 
 
 
 
 
 
 
private Animator anim;
 
 
 
 
 
 
 
public int shopNumber;
 public int songNumber;
 
 
 
 
 
 
 
public Dialogue dialogue;
 
 
 
 
 
 
 
private void Awake()
 {
 anim = GetComponent();
 shopNumber = Random.Range(0, 3);
 songNumber = Random.Range(0, 3);
 anim.SetInteger("shopNumber", shopNumber);
 }
 
 
 
 
 
 
 
private void Start()
 {
 if(songNumber == 0)
 {
 FindObjectOfType().Play("GS1");
 }
 if (songNumber == 1)
 {
 FindObjectOfType().Play("GS2");
 }
 if (songNumber == 2)
 {
 FindObjectOfType().Play("GS3");
 }
 if (songNumber == 3)
 {
 FindObjectOfType().Play("GS4");
 }
 
 
 
 
 
 
 
FindObjectOfType().StartDialogue(dialogue);
 }
}

 

 

 

 

 

 

 

This is my script for dialogueManager:

 

 

 

 

 

 

 

 
 
 
 
 
 
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
 
 
 
 
 
 
public class DialogueManager : MonoBehaviour {
 
 
 
 
 
 
 
public Text nameText;
 public Text dialogueText;
 public float secondsToWaitChar;
 
 
 
 
 
 
 
public Animator anim;
 
 
 
 
 
 
 
private Queue sentences;
 
 
 
 
 
 
 
void Start () {
 sentences = new Queue();
 }
 
 
 
 
 
 
 
private void Update()
 {
 if(Input.GetKeyDown(KeyCode.Space) || Input.GetButton("NextDialogue"))
 {
 DisplayNextSentence();
 }
 }
 
 
 
 
 
 
 
public void StartDialogue(Dialogue dialogue)
 {
 
 
 
 
 
 
 
anim.SetBool("isOpen", true);
 
 
 
 
 
 
 
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));
 
 
 
 
 
 
 
}
 
 
 
 
 
 
 
IEnumerator TypeSentence(string sentence)
 {
 dialogueText.text = "";
 
 
 
 
 
 
 
foreach(char letter in sentence.ToCharArray())
 {
 dialogueText.text += letter;
 yield return new WaitForSeconds(secondsToWaitChar);
 }
 }
 
 
 
 
 
 
 
void EndDialogue()
 {
 anim.SetBool("isOpen", false);
 }
}
 
 
 
 
 
 
 

 

 

 

It works if I use a button like the tutorial, but otherwise it throws this error. Please help. Thanks again!

  • Liked by
Reply
0 on October 24, 2018
sentences.Clear();From AlextoCharArray

 This is the error line.

  • Liked by
Reply
Cancel
0 on October 25, 2018

Solved: I was triggering the text before I was setting the Queue.

  • Liked by
Reply
Cancel