Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question TextMeshProGui NullReferenceException error: how do you fix this?

Discussion in 'UGUI & TextMesh Pro' started by Nathdog18, Nov 13, 2022.

  1. Nathdog18

    Nathdog18

    Joined:
    Sep 10, 2022
    Posts:
    2
    I have been trying to do a score counter in unity2d using textmeshpro recently yet I have became stuck with an error of: NullReferenceException: Object reference not set to an instance of an object
    Score.Update () (at Assets/Scripting/Score.cs:22). I have been stuck on this for 3 hours now, does anyone know anything to help? Thanks in advance!

    Here is my code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;
    public class Score : MonoBehaviour
    {
    public TextMeshProUGUI ScoreText;

    int ScoreCount = 0;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    //Ui Components
    ScoreCount = ScoreCount + (int)Player.Speed * (int)Time.deltaTime;
    ScoreText.text = ScoreCount.ToString();
    //this code is the error ^
    }
    }
     
  2. VadanaPon

    VadanaPon

    Joined:
    Jul 5, 2022
    Posts:
    45
    Make sure you dragged the component into the field in the inspector, or use GetComponent to get in Start
     
  3. Nathdog18

    Nathdog18

    Joined:
    Sep 10, 2022
    Posts:
    2
    Unfortunately, I already have dragged it into the inspector and I have tried getting the component in start, yet it still appears with this error. Would this be because i am on unity version 2021.9f1?