HELP! NullReferenceException: Object reference not set to an instance of an object

Hello,

I created a button “Plus” and wanted to assign onClick function but it was returning “string name” only, not other methods. I found a solution to this: created empty object EmptyGameObject, attached the script “TestManager” to it, then attached the object instead of the script to the onClick field and got access to my method “increase”. But when running the game I get two errors:

NullReferenceException: Object reference not set to an instance of an object
TestManager.Update () (at Assets/TestManager.cs:25)
NullReferenceException: Object reference not set to an instance of an object
TestManager.Increase () (at Assets/TestManager.cs:30)

I know that both are referring to and highlighting the EmptyGameObject but I’m not sure what to do there.

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

public class TestManager : MonoBehaviour {

    public static TestManager instance;

    public Text gameScoreText;
    private int gameScore;


    // Use this for initialization
    void Start () {
       
        gameScore = 0;
    }
   
    // Update is called once per frame
    void Update () {
       
        gameScoreText.text = gameScore.ToString ();
       
    }

    public void Increase (){
       
        gameScore++;
    }
}

Any help would be appreciated! :slight_smile:

The line numbers in your error messages don’t correspond to any code in the script you posted. What line numbers are getting these errors?

For the first error, I’m guessing you haven’t assigned gameScoreText in the inspector. The second error doesn’t make sense due to the Increase method not using any reference types to generate that error, so I’m guessing you’ve made edits to the script and the errors you posted don’t represent the current script.