NullReferenceException: Object reference not set to an instance of an object

I know there have been several questions on NullReferenceExceptions, but the answers I’ve seen don’t seem to fit my problem. In my case, it happens when my player ship is supposed to be destroyed, but then it displays this error instead.

Here’s my code for the segment:
void Die(){
LevelManager man = GameObject.Find(“LevelManager”).GetComponent();
man.LoadLevel(“Win Screen”);
Destroy(gameObject);
AudioSource.PlayClipAtPoint(playerExplode, transform.position);
}

What puzzles me about it is this is exactly what the code in the example is, yet here I am getting this error.

If it makes a difference, I am using Unity 5. If that’s the case, can someone tell me what the updated code should look like there? Or if not, can someone tell me what they see is missing?

Does the GetComponent is written down like this or it was the forum formatting that excluded the GetComponent<LevelManager>()?

[quote=“hypnometal, post:1, topic:26356”]
GameObject.Find(“LevelManager”).GetComponent();[/quote]

You can reference it with just

man = FindObjectOfType<LevelManager>() instead, it is faster and less likely to cause problems in the future

Null reference exception means that you’re trying to find something in the scene that doesn’t exist. 1)Make sure you have an empty game object in your scene called LevelManager and make sure that the game object in the scene is spelled precisely how you spelled it in your script. For example, if your empty game object in the scene is called Level Manager, Levelmanager or levelmanager. The script can’t find it because of spelling errors.

Then check on the game object in your scene to make sure that the LevelManager script is attached to it. Then it should work.

void Die(){
    LevelManager man = GameObject.Find("LevelManager").GetComponent();
    man.LoadLevel("Win Screen");
    Destroy(gameObject);
    AudioSource.PlayClipAtPoint(playerExplode, transform.position);
}

Just to add to the already great responses you’ve received Eric…

I would personally also consider re-ordering those lines, despite whether they were as per the example or not…

If we put the code to one side for a moment and just talk through what’s being requested…

  • Find my level manager
  • Load the Win scene
  • Destroy something
  • Play a sound

Even if this did work, asking for other things to take place after the loading of another scene seems a little odd to me. Typically, all game objects will be destroyed when a new scene is loaded, so, if we still want to use them for anything, it would make sense to have the call to load the level last.

Equally, the Destroy(gameObject); statement which is presumably destroying the player ship game object is called before the following line which then requests access to it’s transform. Yeah, it may still hang around until the next frame (end of the Update loop), but again, just reading it - it doesn’t feel right.

The PlayerClipAtPoint() method will instantiate it’s own AudioSource and will dispose of it automatically once it has finished with the clip, however, you are also accessing the transform of the player ship in this statement, which a) will be destroyed with the LevelLoad() statement, and b) will be destroyed with the Destroy() method after the Update loop.

The following is what I would suggest both reads better and makes more sense.

void Die(){
    LevelManager man = GameObject.Find("LevelManager").GetComponent();
    AudioSource.PlayClipAtPoint(playerExplode, transform.position);
    Destroy(gameObject);
    man.LoadLevel("Win Screen");
}

The cause of you error in this case I believe is that you are calling LevelLoad() which when executed at the start of the next frame destroys all of the game objects from the previous scene. Your Destroy() method then tries to destroy something which no longer exists and, if it got passed that, you are then trying to access the transform component of a game object that no longer exists.

Breaking it down in order to find the specific issue / problem, I would perhaps start with your existing Die() method in the order you have it already, but comment out the last two lines;

void Die(){
    LevelManager man = GameObject.Find("LevelManager").GetComponent();
    man.LoadLevel("Win Screen");
    // Destroy(gameObject);
    // AudioSource.PlayClipAtPoint(playerExplode, transform.position);
}

When you run your game and this method is now called all that should happen is the Win scene should load. If it does then you know that there are no spelling mistakes involved in finding your LevelManager. If it fails again at this point, you haven’t found the LevelManager, so that really narrows down the search.

Assuming the Win scene does load, re-introduce the Destroy() statement and repeat the above. If the game errors you know that you were trying to destroy an object that no longer exists (because the new scene has loaded an all previous game objects were destroyed). If no error presents itself then you are only left with the one remaining line of code to re-introduce, the PlayClipAtPoint() line.

If the game errors again after re-introducing this line then you know it will be because you are trying to access the transform component of a game object that no longer exists. In which case, move this line up prior to loading the next scene and the Destroy() statement, re-apply the above tests.

This is a useful exercise in debugging your code. Below are some links which may also be of use.

I hope this helps :slight_smile:


See also;

1 Like

Yes, it was because I was missing a Level Manager in my game scene. Thank you!

1 Like

i have read the article but basicly i steel have the same error of HYPNOMETAL

void Die(){
	//LevelManager man = FindObjectOfType<LevelManager>();
	LevelManager man = GameObject.Find ("LevelManager").GetComponent<LevelManager>();
	man.LoadLevel("Win Screen");
	Destroy (gameObject);
}

using the above code Gameobject.Find or Find ObjectofType does not work

Do you have a LevelManager game object in your Hierarchy? Perhaps provide a screenshot.

Of course Rob!
About MonoDevelop:
image
About error:
image
About Player and Scripts Folder:
image

In your third screen shot you have an exposed field called Man, if you are not dragging a game object into this field set it to private.

I do not see anything in your scene Hierarchy called LevelManager. Your script cannot find the game object because you dont have one.

Ok Rob. Why do i need a LevelManager in my Hierarchy?

I understant that my unity wont find it that way.

I kind of dont get it why the need of levelmanager in hierchy.

dont really make sense… just it…

WORKING SOLVED tanks Rob

In order for your script to be executed it needs to be attached to a game object in the scene. If it isnt, it will be ignored by Unity.

Glad it is working and you can move forwards again :slight_smile:

of course but level manager could be done with out a game object perhaps?

I will get my self as time goes by… tanks anyway

You could attach your LevelManager script to an existing game object, one that is already in the scene. Might start to get confusing by mixing things up like that though, e.g. when yoy come back to it and are trying to find it yourself (manually).

What is your reason for not wanting a game object in the scene called LevelManager?

At first sight it seems a kind of strange. Java or Asp.Net C# are different just it.

C# is C#, but the way the scripts are compiled, initially by Unity will be different yes.