Getting Null Reference exception when trying to access Dialogue Entry IDs

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
MoshChatterbug
Posts: 1
Joined: Mon Feb 17, 2020 4:40 am

Getting Null Reference exception when trying to access Dialogue Entry IDs

Post by MoshChatterbug »

I've been trying to access the current conversation Dialogue entry id, only to be returned with a null reference exception. I have the Dialogue Manager in the scene.

I'm not sure if I need to specify the exact conversation name but when trying to Debug.Log(dialogueID), I get nothing, despite the conversation tree running.

Code: Select all

   
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;
using System.Linq;

public class SandwichEvents : MonoBehaviour
{
    /// <summary>
    /// This script checks if tags of sandwich assets match the conversation entry id
    /// </summary>
 
    private int dialogueID;

    public void Start()
    {
        // dialogueEntryID = DialogueManager.LastConversationID;
        //actorID = DialogueManager.currentConversationState.subtitle.speakerInfo.id;
        dialogueID = DialogueManager.currentConversationState.subtitle.dialogueEntry.id;
    }

User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Getting Null Reference exception when trying to access Dialogue Entry IDs

Post by Tony Li »

Hi,

Are you sure that the conversation is already active at the time when SandwichEvents.Start() runs? You may want to check DialogueManager.isConversationActive first:

Code: Select all

if (DialogueManager.isConversationActive)
{
    dialogueID = DialogueManager.currentConversationState.subtitle.dialogueEntry.id;
}
else
{
    Debug.LogWarning("No conversation is active. Can't get dialogue entry ID.");
}
If the SandwichEvents component is enabled and its GameObject is active, its Start() method will run as soon as the scene starts. This may be before the conversation starts.
Post Reply