Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Object reference not set to an instance of an object (TextMeshPro)

I’m trying to pick up a magazine and then have it disappear then display a certain amount of number on the screen.

So when I try to pick up the magazine it doesn’t work and there’s a certain problem with the Object reference not set to the object

here’s the code for the value to be added and shown on screen:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

`public class GlobalAmmo : MonoBehaviour
{
    public static int CurrentAmmo;
    public GameObject AmmoDisplay;
    void Update(){
     AmmoDisplay.GetComponent<Text>().text = "" + CurrentAmmo;
    }
}`

Here’s the error that’s been showing:
NullReferenceException: Object reference not set to an instance of an object GlobalAmmo.Update () (at Assets/Scripts/wepons/GlobalAmmo.cs:11)

I think there’s a problem with me using the TextMeshPro UI Component instead of using the regular text component but as the newer version of unity doesn’t have that feature so can someone point me out what’s the code for textmeshpro as i tried but there’s not an object like that

>Solution :

Try to get TextMeshProUGUI component:

AmmoDisplay.GetComponent<TextMeshProUGUI>().text = "" + CurrentAmmo;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading