Error Null Reference

I Keep on Getting the error
“NullReferenceException” on line 18
Capture
the code is exact as shown in a tutorial
can someone help out ?

Hi Hush,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.


See also:

The field is not empty and the code line in which error occurs has not got any link missing. What should i do now ?

Either scoreText or scoreKeeper is null. You are saying the field is not empty, so we will assume that scoreText is not null, which leaves scoreKeeper. If it is null, it means there is no scoreKeeper in your scene.

You can confirm this by adding a check

void Awake()
{
    scoreKeeper = FindObjectOfType<ScoreKeeper>();
    Debug.Assert(scoreKeeper != null);
}

If scoreKeeper can not be found in the scene, this will come up in the console as ‘Assertion Failed’ (or something like that)

yes assetion failed came up
now ?

So, you don’t have a score keeper in your scene. You’ll need to add one

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms