NullReferenceException: Object reference not set to an instance of an object

When I run the code the first button show, but the second no and show me this error.

What does that mean?

Paste in your copy of DialogueUI and we’ll take a look. Don’t forget to format it with the </> button. Also be sure to put a comment with something like //<--- Here on line 41

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;
    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 choiceInstace =  Instantiate(choicePrefab, choiceRoot);
        //Here line 41
        var textComp = choiceInstace.GetComponent<TextMeshProUGUI>();
        textComp.text = choiceText;
      }
    }
  }
}

The textComp is came Null, but the button is TextMeshPro


I still haven’t figured out what’s causing it

Can you post the expanded text of the null reference error, and the script?

Privacy & Terms