getting a unity error even though script is working fine

i made a high score system and it works fine but when i start the game i get a unity error that says “NullReferenceException: Object reference not set to an instance of an object
Score.Start () (at Assets/Score.cs:22)”

this is the script

using UnityEngine;
using TMPro;

public class Score : MonoBehaviour
{
    public double time;
 
    public string scoreStr;

    public float score;
    public float highScore;

    public TMP_Text scoreText;
    public TMP_Text highScoreText;

    public GameObject Player;

    void Start()
    {
        Player = GameObject.Find("Player");
        highScore = PlayerPrefs.GetFloat("HighScore");
        highScoreText.text = "HIGHSCORE:" + PlayerPrefs.GetFloat("HighScore").ToString();
    }

    void Update()
    {
     
        bool isTouchingObstacle = Player.GetComponent<PlayerMovement>().isTouchingObstacle;
     
        scoreStr = (time += Time.deltaTime * 10).ToString("0");
        score = float.Parse(scoreStr);
     
        if(isTouchingObstacle) return;
        scoreText.text = "SCORE:" + score.ToString("0");
     
        if(score > highScore)
        {
            PlayerPrefs.SetFloat("HighScore", score);
        }
    }
}

Did you assign a TMP_Text highScoreText to highScoreText in the inspector? :wink:

3 Likes

apparently i didn’t, thanks

1 Like