Text in Dialogue is not updating

Hi. I followed the course, and double checked the code as well. But when I try to test it in the game, the text in the Irate Guard dialogue is not updating. There is a “NullReferenceException: Object reference not set to an instance of an object” error in the Console as shown below.

Any help to solve this issues would be appreciated. Thank you!

Please fine below the code for the DialogueUI.cs script. Thanks in advance for your help!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Dialogue;
using TMPro;
using UnityEngine.UI;

namespace RPG.UI
{
    public class DialogueUI : MonoBehaviour
    {
        PlayerConversant playerConversant;
        [SerializeField] TextMeshProUGUI AIText;
        [SerializeField] Button nextButton;
        [SerializeField] Transform choiceRoot;
        [SerializeField] GameObject choicePrefab;

        // Start is called before the first frame update
        void Start()
        {
            playerConversant = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerConversant>();
            nextButton.onClick.AddListener(Next);
            UpdateUI();
        }

        void Next()
        {
            playerConversant.Next();
            UpdateUI();
        }

        void UpdateUI()
        {
            AIText.text = playerConversant.GetText();
            nextButton.gameObject.SetActive(playerConversant.HasNext());
            choiceRoot.DetachChildren();
            foreach (string choiceText in playerConversant.GetChoices())
            {
                GameObject choiceInstance = Instantiate(choicePrefab, choiceRoot);
                var textComp = choiceInstance.GetComponentInChildren<TextMeshProUGUI>();
                textComp.text = choiceText;
            }
        }
    }
}

Please fine below the code for the PlayerConversant.cs script. Thanks in advance for your help!

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

namespace RPG.Dialogue
{
    public class PlayerConversant : MonoBehaviour
    {
        [SerializeField] Dialogue currentDialogue;
        DialogueNode currentNode = null;
        private void Awake()
        {
            currentNode = currentDialogue.GetRootNode();
        }

        public string GetText()
        {
            if (currentNode == null)
            {
                return "";
            }
            return currentNode.GetText();
        }

        public IEnumerable<string> GetChoices()
        {
            yield return "I've lived here all my life!";
            yield return "I came here from Newton.";
        }

        public void Next()
        {
            DialogueNode[] children = currentDialogue.GetAllChildren(currentNode).ToArray();
            int randomIndex = Random.Range(0, children.Count());
            currentNode = children[randomIndex];
        }

        public bool HasNext()
        {
            return currentDialogue.GetAllChildren(currentNode).Count() > 0;
        }

    }

}

I edited your post to make the code format properly (Be sure to use the backwards apostrophe just below the escape key on your keyboard like this
```
// This is some code
```
becomes

// This is some code

At first glance, it’s likely that something isn’t set properly in the inspector. We don’t get line numbers in these formatted posts (and I actually had to remove spaces between thelines that the Forums add automatically when it’s not formatted as code), so I can’t be positive which line is line 35. At first glance, I believe it’s

nextButton.gameObject.SetActive(playerConversant.HasNext());

Find line 35 and look at the elements on the line. Find the ones that can be null (usually class references). Look to the coresponding field in your player or AIConversant gameobjects and make sure that these fields are set properly.

First, I apologize for the trouble, It was my first time posting.

I rechecked all the inspector. Everything is added.

And this problem occurred berfore I added:

nextButton.gameObject.SetActive(playerConversant.HasNext());

The console says that the error is in line 35 of DialogueUI.cs , the line is given below:

AIText.text = playerConversant.GetText();

The GetText method is .

public string GetText()
        {
            if (currentNode == null)
            {
                return "";
            }

            return currentNode.GetText();        

GetText() isn’t the issue, or the error would be in GetText().
So either AIText is null or playerConversant is null, but PlayerConversant is assigned in the Start() method…

This leaves either the AIText not being assigned in the inspector (AIText.text) or there not being a PlayerConversant attached to the Player.

Thank u so much. I solved the problem.
I didn’t assign the PlayerConversant to the correct Player prefab. My partner made the previous parts of the project and he made folders and prefabs in a messy way.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms