Why in gods name does this not work?

Please help me, I’m out of ideas. This always returns nullreference:

SerializedObject titleText = new SerializedObject(ui.transform.Find(“instructorSetup/titleText”).GetComponent());
titleText.FindProperty(“text”).stringValue = scenarioName;

For context, instructorSetup/titleText and scenarioName exist. The error is at “FindProperty(“text”)”.

Is this an editor script?
you’re not providing enough information, why not just go ‘.text = screnarioName;’?
what is ‘titleText’ and how is it set?

BTW, please post the full scrupt and use code tags when posting scripts in general

That .text field in a TextMeshProUGUI object is actually a property in a base class TMP_Text. Code snippet here:

    public class TMP_Text : MaskableGraphic
    {
        /// <summary>
        /// A string containing the text to be displayed.
        /// </summary>
        public string text
        {
            get { return m_text; }
            set { m_text = value; m_inputSource = TextInputSources.Text; m_havePropertiesChanged = true; m_isCalculateSizeRequired = true; m_isInputParsingRequired = true; SetVerticesDirty(); SetLayoutDirty(); }
        }
        [SerializeField]
        protected string m_text;

I don’t know if the FindProperty() mechanism works on things with getters/setters; I know the Unity inspector window does not show those types of things, at least it doesn’t in the Unity version I’m using.

Try hack your own quick object up with a “public string text” field and see if you can get it with FindProperty, then make it a custom getter/setter and see if you can still get it. That would perhaps give you some intel about the problem.

If you still can get it when it’s a getter/setter, maybe it’s an inheritance problem and you need to cast to the base class before making the SerializedObject. Just some random thoughts…

1 Like