Unknown NullReferenceException

Whenever I use displaysentence() it throws a nullreferenceexeption
DialogueManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueManager : MonoBehaviour
{

    public Queue<string> sentences;

    // Start is called before the first frame update
    void Awake()
    {
        sentences = new Queue<string>();

    }

    public void dialoguestart (Dialogue dialogue)
    {
        Debug.Log(dialogue.name);
        sentences.Clear();
        foreach (string sentence in dialogue.sentences)
        {
            sentences.Enqueue(sentence);
        }
        displaysentence();
    }
  
   public void displaysentence()
   {
       if (sentences.Count == 0)
       {
           enddialogue();
           return;
       }
       string sentence = sentences.Dequeue();
       Debug.Log(sentence);
   }
    void enddialogue()
    {
        Debug.Log("end");
    }
}

Dialogue

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Dialogue
{
    public string name;
    [TextArea (3,10)]
    public string[] sentences;
}

DialogueTrigger

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueTrigger : MonoBehaviour
{
    public Dialogue dialogue;
    public GameObject cameraref;
    public void triggerdialogue ()
    {
        FindObjectOfType<DialogueManager>().dialoguestart(dialogue);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            Time.timeScale = 0f;
            triggerdialogue();
            Cursor.lockState = CursorLockMode.None;
            cameraref.GetComponent<Camera>().enabled = false;
        }
    }
}

Hi @Tohny123 ,

Would you mind to copy & paste the error message that you’re having (or a screenshot), to see in which line is the error happening?

This specific type of error is because there is some variable that is unassigned at the moment that you’re trying to use it.

If you share with us such message, we can guide you further.

Thank you!

I also used a button to use displaysentence

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

1 Like

i still don’t understand why sentences.count returns null

Only one way to find out: investigate! Line 23 above puts things into sentences, line 35 removes them… put a Debug.Log() before and after each one and display the count. Is this code even running? etc.

It turns out that when I use a button for displaysentence() it doesn’t have the string queue, so I changed it so that whenever I use the button, I make a reference to the dialoguemanager, then to the queue, so then I can use that reference to display the sentences I need.
Thank you!

2 Likes