(Solved)NullReferenceException error in enemyBehaviour script when adding score with scoreKeeper

Ok, so when I added my ScoreKeeper script something went wrong. My scripts look identical to Brice’s (to me at least!), but the score never changes in-game. In addition, if I move my “scoreKeeper.Score(scoreValue);” line above my “Destroy(gameObject);” line in my enemyBehaviour script, the enemies never get destroyed, so there must be a real problem with that line (35 in the screenshot below).

As always, any help would be massively appreciated!

You dont appear to be getting a reference to.your UI.Text object.

In line 11 where you use GetComponent (ScoreKeeper), is this scriot actually attached to domething that has a UI.Text object?

Good question. That was my instinct. It’s attached to this text box, which I created as a UI component in the hierarchy. Is that what you need to know?

That text box is called “Text”, yet on line 14 in the other script (EnemyBehavior) you are asking it to find something called “Score”.

1 Like

That’s brilliant! I don’t know how you keep track of all these similar words in your head! I think I’m going to have to start calling some objects things like "Frank, just so I don’t get confused!
Well done! And thank you again!
I really appreciate your help. Have a groovy weekend!

1 Like

You are more than welcome, glad it is now resolved and you continue. :slight_smile:

Hi guys,

I know this post is months old but I have hit the same problem. It has been driving me crazy! I have gone through the code over and over, even with someone with much more coding experience then me, and we couldn’t figure it out. I have read the manuals on this NullExeption and have had no solution. I just read this and this was my problem! I just wanted to say thank you for posting the question and thank you for helping me move past this problem! BTW I have learned a lot while stuck on this problem!:slight_smile:

1 Like

Awesome to hear :slight_smile:

Privacy & Terms