(Solved)Yet another NullReferenceException: Object reference not set to an instance of an object

I’m stuck here as well, my game is also running but my enemies will not destroy if I have the line scoreKeeper.Score(scoreValue); in my EnemyBehavior script.

The error I’m getting is:
NullReferenceException: Object reference not set to an instance of an object
EnemyBehaviour.OnTriggerEnter2D (UnityEngine.Collider2D collider) (at Assets/Entities/Enemy/EnemyBehaviour.cs:41)

The error occurs when the enemy should be destroyed.

My EnemyBehaviour script looks like this. If I comment out line 41 the enemies can be destroyed but the score will not update.

If I understand correctly that should pass the scoreValue to the int points in the ScoreKeeper, and the ScoreKeeper script is attached to the text box (which is named Score) is this correct?

My ScoreKeeper looks like this:

And my Score UI looks like this

Any help on this one would be much appreciated!

1 Like

You’ve used a lower case ‘s’ for the start method.

Should be

void Start ( )
{
    //Your code here
}
2 Likes

Yep, so that method doesn’t get run, as it isn’t Start() - as such, scoreKeeper is never initialised on line 15, thus, null reference exception.

Nice spot @pokedev8 :slight_smile:

A second pair (and third) pair of eyes has made all the difference, I would not have spotted that typo! Thanks for the help :slight_smile:

1 Like