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!