Game works, but I'm still getting "Object reference not set to an instance of an object

Hello All!

This is my first post, so hopefully I’m doing it right. While I was in the process of Managed my next states(Lesson 26), I noticed I kept getting the error message:
“NullReferenceException: Object reference not set to an instance of an object
AdventureGame.Start () (at Assets/Scripts/AdventureGame.cs:19)”

And

“NullReferenceException: Object reference not set to an instance of an object
AdventureGame.ManageState () (at Assets/Scripts/AdventureGame.cs:30)”

Line 19 is textComponent.text = state.GetStateStory();
Line 30 is var nextStates = state.GetNextStates();

I didn’t see anything wrong with my code, as it was exactly the same as the one posted on the sections github.

I replaced my State.cs and AdventureGame.cs with the one’s of the github as well just to test, and I’m still getting the same issue, so I’m assuming it has to be something in Unity.

Also, when I run the Attach to Unity and Play option in Visual Studios 2017, it’s saying that everything related to state is NULL. But again, the game works, so I’m not sure why it’s shows as NULL here. I’m wondering if there was something else I had to do with the State.cs to make sure everything is defined first?

I’m using Unity 2018.1.1f1 if that helps.

Thanks so much!

It looks like state is not being initialised.

It should be something like:

class state = new (class)

e.g. GameState state = new (GamesState);

I am not on that course, so no idea what it might be exactly.

If no joy with that, then either paste AdventureGame.cs here (if not too big), or stick it on something like Pastebin and link that here.

Good luck!

:smiley:

Hi,

With each of these state instances you are referencing the next states by reference through the Inspector, e.g. where you drag your Scriptable Objects in. If you haven’t done this you will receive a nullreferenceexception as the instance is null.

Thanks for the info Rob. I double checked my states to make sure they all had a reference, and they appear correct. Currently I only have these 3 states:

State%203State%201State%202

And my game object:
Game%20Object

Again I’m using the actual .cs files from the lesson’s github to make sure it wasn’t the script:


Any other areas where I can check or something I overlooked?

I started my script back over from a few lessons ago and haven’t had the issue yet. I also just realized that the state = startingState; part of the code on gitHub is commented out, which was most likely causing the errors.

1 Like

If you didn’t have that line, the compiler wouldn’t know what it was supposed to get next, so yes, most likely your issue, and would certainly cause a nullreferenceexception, as state would be null.

Note, you can just copy/paste your code into the forum and then apply the code formatting characters, both before and after the code, makes it a lot easier for those who offer to help you. :slight_smile:


See also;

Thanks for the formatting advice Rob! I guess I just assumed the github final code was what I was working towards, next time I’ll double check for anything commented out.

Cheers!

1 Like

You’re more than welcome Zac :slight_smile:

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

Privacy & Terms