Null Reference Exeption

Hello,
There are quite a few topics of this already so I know I may be repeating myself but I didn’t really understand the answers and wanted to ask the question for myself.

Everything is working fine except whenever I click on play I get a null reference exception:
image

The code for this exception is:

I’ve made sure that my scripts are in the right place and not doubled or missing. If I click play the score doesn’t change aswell, it stays as the place holder text. I tried adding the GameSession file to the text but then it deletes it’s self because there’s more than 1 singleton.

Not really sure what to do other than copy code from others in similar topics but I don’t really understand it and so would like to know why I would need to change my code to something else as it looks like Rick’s code and he didn’t get an error :(.

Hi Froggy,

MonoBehaviour does not have the same colour as your classname. This indicates that there is very likely a problem with Visual Studio. Please watch lecture “Fixing Visual Studio Problems” (currently #4).

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. GetComponent looks for a component assigned to a specific game object, here: to the same as the ScoreDisplay component. Are both components the code is looking for in the Start method attached to the same game object as the ScoreDisplay component?

Remember you can also look at the lecture code changes via the link in the Resources of each lecture.

My code looks like that of Ricks. And I have my ScoreDisplay.cs on my Score Text and my GameSession.cs on a GameSession gameobject.

Monobehavior isn’t the same colour as any of my classnames. Could that be due to me using an older version of VS? I will look at the video now.

Edit:
I’ve watched some of the video and my VS is working well now, however the error still comes up :frowning:

Could you sharea screenshot of the Inspector with the ScoreDisplay component?

Yes, here you go:

Don’t worry about the white strip with rubber, pen … its what I use to take screenshots.

Thank you. I don’t know if there are any other scripts assigned to the game object but check if there is also a GameSession component in the same Inspector. If there isn’t any, you found the issue.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Double check if Rick gets the GameSession component via GetComponent.

Thank you, I have looked at Rick’s code already. I added a GameSession Component but due to the singleton in the script, because there are 2 gamesessions in the game it destroys the text. I’ll check the game Session code on the lecture changes.

Edit:
Yes all my code looks the same, but whenever I load my game the score text gets destroyed :frowning: and I’m not sure how to prevent this.

I think the score doesn’t change because you use GetComponent, instead of FindObjectOfType.
gameSession = FindObjectOfType();

However, I too would like to know why there is a Null Exception, as I am getting it too. However, I am getting it at Level.cs in the LoadGame method for the line
FindObjectOfType().ResetGame();

EDIT: I just needed to add Game Session component to Start Menu

How are you getting on with this, @Froggy?

Pretty sure I figured it out :smiley:! In my code i hade a reference to my gameSession script but I used GetComponent() so it Destroyed the component attached to my text which destroyed my text. All I had to do was chnage this to FinObjectOfType() and it fixed the issue!!

Yup you were right thank you very much

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

Privacy & Terms