Question about NullReferenceException and Singleton

Hey,
I was wondering, when I run the game without the Game Session game object in the scene the game runs totally fine but I keep getting this exception NullReferenceException. If I add the Game Session game object to all the scenes (prefab) everything is ok.

  1. What’s the reason behind getting this exception when Game Session game object is missing from the scene?
  2. How is that possible that without the Game Session game object the game and score counting runs fine? I noticed that when I click play it generates DontDestroyOnLoad object, but how Unity knows to assign to the scene this object from the GameSession.cs script if the Game Session game object does not exist in the scene, how does Uniy / C# finds the GameSession type?

TIA

2 Likes

Imagine a relay race, there’s one baton and the only runner that has it is the initial runner, when the first athlete reaches the second participant the baton will be passed down. What would happen if the race starts at the second runner? There will be no baton to pass.

This is the same. The runners are the scenes and the baton the Game Session script, if you start in a scene that has a Game Session class it will be passed down to the next scene you load. If you run your game in a scene that has no “baton” then you’ll get an error because there’s is none, or null, hence the name of the error; “NullReferenceException”. This is probably called from the Enemy script, which is the one that tries to access it.

How does Unity knows? Because you told Unity in your code:

    private void SetUpSingleton()
    {
        int numberGameSessions = FindObjectsOfType<GameSession>().Length;
        if (numberGameSessions > 1)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject); //This line
        }
    }

Why does your game run fine? Try running your game in a scene with no Game Session script on it, you’ll notice that it works, but the score will remain at 0 or whatever default value you set it to.

Thanks for the reply.
But how can we use FindObjectsOfType() if we did not instantiated a Game Session game object in the scene? How this script (GameSession.cs) even gets executed?
In the screenshot below I get two exceotions.
If I enable the Game Session game object in the Start Menu scene and the Game scene I don’t get any exceptions at all. If I have this Game session object only in one of them I get two exceptions.

And as you can see below the hierarchy in the Start Menu scene, it has a Game Session object under DontDestroyOnLoad even though is set it to be disabled.

And then when I click Play and go to the Game scene it does not generate the Game Session object.

I got bit confused :neutral_face:

You can’t, that’s why you get those error messages. The description is basically saying; I’m trying to use something but that something isn’t here.

About your other issue. The DontDestroyOnLoad method won’t be called if the Game Session object is disabled, this mean the object will be destroyed after loading another scene. Enable the Game Session object to prevent further errors.

If this doesn’t solve your problem then it’s a matter of code, compare it to Rick’s, if you can’t find anything and your problem persists come to the forums and paste your code.

It works for me if I enable the Game Session object in the Start Menu scene and the Game scene.
But, if I enable this object only in the Start Menu scene I get Exceptions when I go to the Game scene, and the game does not run properly. That is what I am trying to figure out, why is it happening? Why doesn’t it generate the Game Session?

*** I haven’t posted the reply by the time I wrote it :point_up_2: so I will leave it up there if someone’s interested, anyways, I think I might figured it out - if the Game Session is enabled only in the Start Menu scene, it remains in the Game scene till the point that the LoadGame() method from the Level.cs executes, at that point it runs the ResetGame() method which destroys the one and only GameSession object that we currently have, then the ScoreDisplay.cs tries to access the GameSession object in the update method so it ends up with infinite Exceptions.
Do you think I got it right?

Yes, that was your issue and had the right to be confused, this is a very weird approach, not sure what Rick was trying to do here, it kinda kills the whole purpose of having a singleton.

1 Like

Hi,

There is a bug in the “singleton” implementation. Rick fixes it in a later video.

In your “singleton”, call gameObject.SetActive(false); in the same if-block where Destroy(gameObject); gets called. That should fix it.


See also:

2 Likes

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

Privacy & Terms