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

Hi! I’m new here and also with Unity and programming in C#… So I have some trouble.

I want to take a font asset (TMP_FontAsset of TextMeshPro) from the inspector using my class Manager, and then change every text with the FontText script attached, so its font is the one I took from the inspector.

Here is what I use for this in Manager.cs:

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

public class Manager : MonoBehaviour {

    // ...

    public static Manager Instance { set; get; }

    [Header("Global Text")]
    public TMP_FontAsset GlobalFont;

    // ...

}

And this is the FontText script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class FontText : MonoBehaviour {

    private TextMeshProUGUI m_TextMeshPro;
    private TMP_FontAsset m_Font = Manager.Instance.GlobalFont;

    void Start () {
        UpdateFontText();   
    }
 
    void UpdateFontText () {
       
        if (m_TextMeshPro == null && GetComponent<TextMeshProUGUI>() != null) {

            m_TextMeshPro = GetComponent<TextMeshProUGUI>();
  
            m_TextMeshPro.font = m_Font;

        }

    }
}

So the thing is that with this code it works, it changes the font, but it gives me a NullReferenceException: Object reference not set to an instance of an Object

I hope someone could help me, I have no idea why this happens… I need the code to be correct even if it apparently works. I tried to assign the font (m_Font = Manager.Instance.GlobalFont; ) in the Start() function or in the Update(), but then the font doesn’t change and the error is still there… Any ideas?

Thank you very much!

Edited:

Here are the lines of the error:

NullReferenceException: Object reference not set to an instance of an object
FontText…ctor () (at Assets/Scripts/FontText.cs:9)

Please paste the entire error message, including the lines that appear below that one when you select it. It includes vital information, especially the line number of the error.

at line 20 of FontText, put this:

Debug.Log(m_TextMeshPro);

if that comes up as Null… then there is your problem. the component does not exist.

Tip:
Always do null refernce checks before using variables, unless, you apply this:

[RequireComponent(ComponentNameHere)]

Even then, its not a bad idea. :wink:

Read line 17… there is a null check there.

It would be good to see what line the error is on and the entire error.

I’m wondering if Manager.Instance is null though.

Hi, thanks for the answers! I edited the post with the lines of this error.

Yeah, then I stand by what I said.

Manager.Instance is probably null.

Your Manager class needs

Instance = this; in the Awake method.

Then in FontText you should have

m_Font = Manager.Instance.GlobalFont;

In start if you really want to go that route, but honestly you could just assign the font straight from the manager class

m_TextMeshPro.font = Manager.Instance.GlobalFont;
1 Like

Oooh! Thank you Brathnann, thank you very much!
I was using too much lines for something that simple, I just took the last line you wrote and finally it works perfect, with no errors!

Love you! :_)