I'm trying to create a prefab to instantiate things in a research tree, but when I try to set the text for the TextMeshPro text and the sprite for the Image, I get the following exception:
ResetScriptObjects: failed to reset objectNullReferenceException: Object reference not set to an instance of an object
Research..ctor () (at Assets/Scripts/Research.cs:27)
Here is the relevent code:
[SerializeField] private string DisplayName = "";
[SerializeField] private Sprite image;
[SerializeField] private TextMeshProUGUI m_displayText;
[SerializeField] private Image m_image;
public Research()
{
Debug.Log("Set stuff");
m_displayText.text = DisplayName;
m_image.sprite = image;
}
Research()
is a constructor for public class Research : MonoBehaviour
and is attached to a prefab. This prefab has two child components: One with an Image
, and one with a TextMeshPro Text
. I've attached those two to the m_displayText
and m_image
, but when I try typing something in the DisplayName
, or assigning a sprite to image
, I get the error.
EDIT: I've tried replacing the constructor with Awake() OnEnable() and Start(), to no avail. I'm using linux ubuntu if it helps.
m_image.sprite = Image
is meant to bem_image.sprite = image
, since what you have above won't compile. Try updating this question to include a Minimal Complete Verifiable Example of the problem that does not use a constructor. \$\endgroup\$