1

I am facing an issue with a NullReferenceException in my code. I've created a LevelManager script to manage the different scenes of my game. Inside this file ScoreKeeper object is called. This ScoreKeeper is managed by a Singleton pattern to ensure a connection between the different scenes. Everything worked so far, until I tried to call the ResetScore method (when launching a new game (to avoid starting the game from the previous score)).

What I've already done :

  • Ensure a ScoreKeeper prefab is available in each Scene

enter image description here

  • Ensure the Awake() method is called

enter image description here

  • Manage the Singleton Pattern

enter image description here

Everthing worked perfectly when switching from from GameOver Scene to MainMenu Scene, but the bug appear when trying to load the Game Scene

Below is a screenshot of the object's list before trying to reach the Game Scene :

enter image description here

I really don't understand why this issue appear as a ScoreKeeper is well visible in the DontDestroyOnLoad section :(

Here is the error message that pop's up :

enter image description here

Thanks in advance for your clues

5
  • 3
    Please include your code as text, not images. Commented Aug 27, 2024 at 9:22
  • 1
    Don't post images of code and error messages, include the actual text please. Commented Aug 27, 2024 at 9:22
  • 1
    Though my guess would be to check if the Awake method is being called before the LoadGame method. Commented Aug 27, 2024 at 9:23
  • I hate when answers get closed prematurely. Your problem is that you have set up your singleton incorectly. Check out my answer to your problem here Commented Aug 27, 2024 at 9:34
  • 1
    @Jay It's been re-opened. Feel free to convert that to a proper answer Commented Aug 27, 2024 at 9:35

1 Answer 1

2

You are misusing the singleton pattern.

The correct use would be this:

In ScoreKeeper

public class ScoreKeeper : MonoBehaviour
{
    public static ScoreKeeper Instance { get; private set; }

    private void Awake()
    {
        if (Instance != null && Instance != this) // if we are the instance this is fine
        {
            Destroy(this);
            return;
        }

        Instance = this;
        DontDestroyOnLoad(this);
    }
}

Then when accessing it, don't hold on to a reference, just do this each time.

ScoreKeeper.Instance.ResetScore();

Your error was occurring because LevelManager's reference was somehow becoming null, likely because loading into the game made the original ScoreKeeper delete itself due to the error in its awake logic.

This should now be safe either way... but perhaps best not to hold on to a singleton instance reference anyway, or you could forget it's a singleton not a normal variable.

Also important to note. You Must set your singletons to run first in the Execution Order or you will run into problems trying to access them.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your feedback, I've updated to put ScoreKeeper.Instance.ResetScore() on each script which required a ScoreKeeper and it works just fine. I knew a singleton must run first in the execution order to avoid any issue accessing it, that's why I put it in the awake method. Is that ok, or do you advice any other action to ensure it runs first? When I look in the link you shared it seems I made it right (I hope). Thanks for your advice
@MaxenceHermand My appologies, I inserted the wrong link. It may not be urgent but this shows what i was trying to talk about, it's a project setting which causes some scripts to run before others - even in the same unity event phase

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.