Making 'TextMeshPro -Text (UI)' a [SerializeField]

NullReferenceException: Object reference not set to an instance of an object
AdventureGame.UpdateState (State nextState) (at Assets/Scripts/AdventureGame.cs:49)
AdventureGame.Start () (at Assets/Scripts/AdventureGame.cs:19)

So I got this error while switching my Text objects into TextMeshPro - Test (UI) objects in Unity.

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

public class AdventureGame : MonoBehaviour
{
    //storyText corresponds to textComponent from the video, 
    //sceneDecription is just an extra piece of text that does the same thing
    [SerializeField] Text sceneDescription;
    [SerializeField] Text storyText;
    [SerializeField] State initializingState;

    State state;

    void Start()
    {
        UpdateState(initializingState);
    }

    void Update()
    {
        ManageState();
    }

    private void ManageState()
    {
        var nextStates = state.getNextStates();

        try
        {
            if (Input.GetKeyDown(KeyCode.Alpha1))
                UpdateState(nextStates[0]);
            else if (Input.GetKeyDown(KeyCode.Alpha2))
                UpdateState(nextStates[1]);
            else if (Input.GetKeyDown(KeyCode.Alpha3))
                UpdateState(nextStates[2]);
        } catch (IndexOutOfRangeException e) {
            
        }
    }

    private void UpdateState(State nextState)
    {
        state = nextState;

        //The error comes here
        sceneDescription.text = state.getSceneDescription();
        storyText.text = state.getStateStory();
    }
}

and - no Text objects were being passed to the serialized variables;
I removed the Text objects and created separate TMP objects, but not passing them into the serialized field.


But then I realized that I cannot pass TMP objects into where I should put Text instead.

    [SerializeField] Text sceneDescription;
    [SerializeField] Text storyText;

What should I replace the Text variable type into?
Thanks!

Hi,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.

At the top of your code, add using TMPro;. The type for your variables is TextMeshProUGUI.

Did this help?


See also:

3 Likes

Yes! It worked perfectly - :grin:

But could you share me where to get those kind of Unity informations?
The Unity user guide didn’t tell me, the Stackoverflow usually won’t contain exactly what I want… (duh)
Thank you for so much help :slight_smile:

1 Like

What’s the name of the text mesh pro component that you added to your Inspector? In the past, it was named “Text Mesh Pro UGUI”. That’s why I know that the type is TextMeshProUGUI.

And I know the namespace from the API.

image

image

2 Likes

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

Privacy & Terms