Confused by NullReferenceException

I have declared a public variable in my script and set it to a Text object in the Inspector but when I try to initialize it I get the NullReferenceException error saying it hasn’t been set to an instance of an object. I assumed that setting it in the inspector would have done that. Here is my code:

**public Text Story;**

private void Start() {

    Scene scene = SceneManager.GetActiveScene();

    **if (Story.text != string.Empty) { subText = Story.text; }**
    **else { Story.text = subText; }**

    place = scene.buildIndex;

}

All the searching I have done and going over the lessons again seems to indicate that this should work.
STUMPED!

Hi @MASmullin,

So, I see three variables within your Start() method.

  • Story
  • subText
  • place

Obviously I don’t have all of your code so I cannot see what you may be doing with these outside of the Start() method.

If Story is the public variable which you have dragged your text object onto in the Inspector that should be ok, but check.

So, what are the other two variables? Where are they being defined/instantiated?

Thank you for your input, Rod. I should have mentioned that I am using Unity 5, if that makes any difference. Yes, it is the Story element in the script that is throwing the error. Here are some screen shots of the Inspectorl

And here is one of the script.

Not spotting anything obvious.

In the Start() method in GameEngine you are trying to access the Story text ui object, do you have any scenarios where the game engine scipt is attached to a scene that doesnt have the Story texr ui object? Or, where you disable/hide it?

No. All my scenes have the Story object in them and they are not hidden or disabled. There is one scene, which is the lose scene which was left blank with the intention of setting it at run time which is the reason for that black of code. I am trying to detect it and set it at run time.

@MASmullin, how are you?

It might not be the problem, but sometimes mixing static and non-static variables inside the same method can cause some problems, have you tried changing subText to a non-static variable and see how it behaves?

I have a feeling that what is not set to an instance ia the subText and not the Story

if this don’t work, you could also give a try to changing the Story value manually (changing it to private instead of public), with Story = GetComponent<Text>() if it is within the same GameObject or Story = GameObject.Find("NameOfTheGameobjectWithText").GetComponent<Text>(), it should solve problems that may appear if we drag a wrong instance or prefab to the inspector.

By the way, cool modifications you are doing to the game, I’m looking forward to playing it :smiley:

1 Like

If the Story object is definitely in the lose scene, e.g. you could find it in the scene view and you’ve dragged it into that variable then that shouldn’t be causing you a null reference exception. Do you have any default text in the text ui object within the Inspector? For example, if it was for a player’s score I may leave “Score” in it, even if I was over riding this in code, mainly just so I can find the object really easy in the scene view.

Are you able to test just the lose scene, and perhaps comment out the if / else block to check that it either definitely is or isn’t that Story object causing the problem.

It’s obviously hard to check/test remotely without the full project/code etc. I can’t see anything wrong specifically with the code, but I have to make a lot of assumptions this end. If it were me I would want to comment those lines out to see if the error goes away, but this may not be so straight forward if you are requiring them on every scene in order to get to the lose scene. I suppose you could skip over them with another if statement using your place variable (if you moved it above), e.g. if the build index is not the lose scene, run that if / else block. That way it would effectively step over that code when you are on the lose scene. If you still get the null reference exception it shouldn’t be related to the Story object.

Privacy & Terms