I am trying to create a dialogue system following the Brackeys tutorial, and then edited to create a system for a conversation between 2 or more actors.
Now is throwing a reference exception for a class with no gameobject/ monobehaviour.
Here’s the the Dialogue Manager with the problem lines commented out under StartDialogue():
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogueManager : MonoBehaviour
{
[SerializeField]
private AudioManager audioManager;
public GameObject dialogueBox;
public GameObject characterPortraitBox;
private Queue<string> sentences;
public TextMeshProUGUI nameText;
public TextMeshProUGUI dialogueText;
// Start is called before the first frame update
void Start()
{
sentences = new Queue<string>();
dialogueBox.SetActive(false);
}
private void Update()
{
AdvanceDialogue();
}
public void StartDialogue(Dialogue dialogue)
{
dialogueBox.SetActive(true);
Time.timeScale = 0.01f;
/**Fix Null Reference Exception on commented code bellow**/
//nameText.text = dialogue.characterName;
//characterPortraitBox.GetComponent<Image>().sprite = dialogue.charaterPortrait;
//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));
}
public void EndDialogue()
{
Time.timeScale = 1f;
Debug.Log("End of Conversation");
dialogueBox.SetActive(false);
//Steps to close out dialogue box.
}
IEnumerator TypeSentence(string sentence)
{
dialogueText.text = "";
playCharacterSFX();
foreach (char letter in sentence.ToCharArray())
{
dialogueText.text += letter;
yield return null;
}
}
private void playCharacterSFX()
{
if (nameText.text == "Daedalus")
{
audioManager.Play("Daedalus Typing");
}
if (nameText.text == "Wisp")
{
audioManager.Play("Wisp Typing");
}
}
private void AdvanceDialogue()
{
if (Input.GetKeyUp(KeyCode.Return))
{
DisplayNextSentence();
Debug.Log("Player pressed enter/return");
}
}
}
Here is the Class it is referencing:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialogue
{
public Sprite charaterPortrait;
public string characterName;
[TextArea(3, 10)]
public string[] sentences;
}
And here is the class that triggers the dialogue:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour
{
public Dialogue[] conversation;
private Dialogue dialogue;
void Start()
{
//TriggerDialogue();
}
public void TriggerDialogue()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
public void OnMouseUp()
{
TriggerDialogue();
}
}
Any advice?