Null Refrence exception in Text 101

hey all, I’ve been doing the Text 101 course and I’m currently on the module “Public methods and return types” and Although it seems like I have everthing in order I’m getting a null refrence exception because Unity is not able to find my “GetStateStory”.

I’ve hooked up Storytext to the inspector, what is going on here?

2018-09-04%2020_48_04-Window

2018-09-04%2020_38_11-Window

2018-09-04%2021_11_02-Window

UPDATE; I realised that “Story Text” had the wrong capitalization and also a space, and changed it acordingly, then hooked it back up to the inspector. But it still is giving me the exception…

Hi Clint,

It’s a bit difficult to tell from the screenshots, could you share your project files and I will take a quick look for you.

The forum will allow uploads of up to 10mb, so as long as your project files are smaller than that when zip you can just upload them into your post, if for some reason your project files are large than this, please use a service such as Google Drive or Dropbox and share the URL instead.

For reference, you can copy/paste you code directly into your posts on the forum and then just apply the code formatting prefix/suffix, this makes it a lot easier for those who help you as they can copy/paste chunks of code back to you without having to type it all out. Screenshots are of course useful for details from the Unity editor, or errors messages.


See also;

(This is Adventuregame.cs)

public class AdventureGame : MonoBehaviour {

     [SerializeField] Text textComponent;
	 [SerializeField] State startingState;
	 
	 int[] oddNumbers = {1, 3, 5 ,7 ,9};
	 
	 State state;

      // Use this for initialization
    void Start () {
		   state = startingState;
           textComponent.text = state.GetStateStory();
		   Debug.Log(oddNumbers[3]);
	}
	
	
// Update is called once per frame
	void Update () {
		
    }
}

(This is state.cs)

[CreateAssetMenu(menuName = "State")]
public class State : ScriptableObject {

	[TextArea(14,10)][SerializeField] string storyText;
	
	public string GetStateStory()
	{
		return storyText;
	}
	
}

(and the attached project files)

Assets.zip (28.7 KB)

1 Like

Hi Clint,

Thanks for trying to apply the code formatting, it’s actually this character ` you need as opposed to though, I’ve corrected it for you :slight_smile:

Regarding your error message…

I downloaded your assets and create a new project and copied them all into the assets directory.

I wasn’t sure which scene you were having the issue with, so I open AdventureGame and ran it, I saw the NullReferenceException error so assumed it was this one.

The error specifically relates to this line of code;

textComponent.text = state.GetStateStory();

which is in the Start method within the AdventureGame.cs script.

So, what can we determine based on this error, well, we know that Unity is unhappy because we are trying to access a member of an object. If we look at the statement above there are two objects, the textComponent and the state, we need to know which one is causing us the problem.

If we think about why these objects may be null, in both cases these are serialized fields which you are exposing in your class so that you can add a reference to another object within the Unity editor, via the Inspector. So, we turn our attention to this.

First, we need to know where this script is attached, so we can find the GameObject(s) in question. Working my way down through each GameObject in the Hierarchy I see the Game GameObject which has the AdventureGame.cs script as a component. If we look at this in the Inspector we can see this;

image

The Text Component looks good, you have created the reference by dragging the Story Text GameObject across, however, Starting State says “None”, so we are missing a reference. This would create a NullReferenceException.

To resolve;

  • select the Game GameObject in the Hierarchy
  • select the Assets root folder
  • drag your startingState ScriptableObject from Assets to the exposed Starting State field within the Inspector
  • run the game


(maxmise the video for greater clarity)

You will now see that you do not receive the NullReferenceException error because you have created the missing reference.

Hope this helps :slight_smile:

2018-09-05%2016_17_52-Window

IT WORKS! :smile: Thanks a lot, I was stuck on that for awhile!

1 Like

You’re are very welcome :slight_smile:

1 Like

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

Privacy & Terms