Page 1 of 1

Getting Null Reference exception when trying to access Dialogue Entry IDs

Posted: Mon Feb 17, 2020 4:52 am
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;
    }


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

Posted: Mon Feb 17, 2020 8:25 am
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.