NullReferenceException: Object reference not set to an instance of an object GameController.Start ()

public Text currentScoreT, highScoreT, currentLevelT, nextLevelT;

    public Image levelProgressI;

    int score, level = 1, highScore = 0;

    void Start()
    {
        if(PlayerPrefs.GetInt("ilkGiriş", 0) == 0)
        {
            PlayerPrefs.SetInt("level", 1);
            PlayerPrefs.SetInt("highScore", 0);
            PlayerPrefs.SetInt("toplamOyun", 0);
        }

        score = 0; 
        level = PlayerPrefs.GetInt("level", 1);
        highScore = PlayerPrefs.GetInt("highScore", 0);

        currentScoreT.text = score.ToString();
        highScoreT.text = "BEST: " + highScore.ToString();
        currentLevelT.text = (level).ToString();
        nextLevelT.text = (level + 1).ToString();

        PlayerPrefs.SetInt("toplamOyun", PlayerPrefs.GetInt("toplamOyun", 0) + 1);
    }

ERROR:

NullReferenceException: Object reference not set to an instance of an object
GameController.Start () (at Assets/GameController.cs:27)

You should at least post the entire contents of GameController.cs, so that we can know from which row the exception is coming from. A little more context about when you’re getting the error also wouldn’t hurt…

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

public class GameController : MonoBehaviour
{
    public Text currentScoreT, highScoreT, currentLevelT, nextLevelT;

    public Image levelProgressI;

    int score, level = 1, highScore = 0;

    void Start()
    {
        if(PlayerPrefs.GetInt("ilkGiriş", 0) == 0)
        {
            PlayerPrefs.SetInt("level", 1);
            PlayerPrefs.SetInt("highScore", 0);
            PlayerPrefs.SetInt("toplamOyun", 0);
        }

        score = 0; 
        level = PlayerPrefs.GetInt("level", 1);
        highScore = PlayerPrefs.GetInt("highScore", 0);

        currentScoreT.text = score.ToString();
        highScoreT.text = "BEST: " + highScore.ToString();
        currentLevelT.text = (level).ToString();
        nextLevelT.text = (level + 1).ToString();

        PlayerPrefs.SetInt("toplamOyun", PlayerPrefs.GetInt("toplamOyun", 0) + 1);
    }

    void Update()
    {
       
    }

    public void Scoree()
    {
        score += level;

        currentScoreT.text = score.ToString();

        levelProgressI.fillAmount = (float)(score / (score * 35));
    }

    public void Levell()
    {
        level++;
        PlayerPrefs.SetInt("level", level);

        currentLevelT.text = level.ToString();
        nextLevelT.text = (level + 1).ToString();
    }
}

Error is the same. I have text UI objects in canvas and I want to write somethings with code but I cant. For example when I remove the code which makes the error then below that makes the same error. if I remove all text code then it fixes.

It looks like you haven’t assigned currentScoreT, highScoreT, currentLevelT and nextLevelT through the inspector. You need to drag and drop Text components into each one.

NullReferenceException happens when you are trying to access something on an object that doesn’t exist. So when you call currentScoreT.text, but currentScoreT does not exist, the exception is thrown.

I did that too.

You probably have multiple instances of GameController in your scene. Try typing “GameController” in the search box of the Hierarchy view and see if you find more than one of them.

1 Like

It worked. Thank you.

1 Like