Question on traditional global singleton solution

Hello!

I have finished the course and in this lecture it is explained how to create singletons in two ways, one is publicly accessible and the other is just a bit more private. I’m trying to make the ScoreKeeper a true global singleton for easier use as stated in the next lecture.

So I copied the Singleton pattern code from AudioPlayer along with the “GetInstance” method but it requires an instance of an object to do so. This will throw the null reference error:

ScoreKeeper scoreKeeper;

void Start()
{
  scoreText.text = scoreKeeper.GetInstance().GetScore().ToString();
}

And that happens because the variable scoreKeeper is null because no value has been initialized on that variable. The way I fixed it was by not creating a variable and just using the ScoreKeeper class itself:

void Start()
{
  scoreText.text = ScoreKeeper.GetInstance().GetScore().ToString();
}

But that code will also throw an error stating that the method GetInstance needs an instance of an object, so I made the GetInstance method a static method as well:

public static ScoreKeeper GetInstance()
{
  return instance;
}

That way you won’t need an instance to access that method.

But my question is, is this how our loved Gary intended it to be? Because that last part is very important part and it is not explained in the lecture, or am I missing something?

Hi OnixFang,

Since your solution is a bit different than Gary’s, it does not matter how Gary intended it to be. If you are interested in his solution, use his. If you are interested in making your own solution work, don’t worry about Gary’s.

Your GetInstance method is perfectly fine. However, it does not solve the problem with the NullReferenceException error. instance could still be null. Your code never checks its value, and, from what I see, it never assigns any value to instance, hence instance remains null and will always throw a NullReferenceException if you try to access the non-existent object.

Assign a reference, and your problem will very likely be solved.

Take Unity’s execution order into consideration when assigning the reference. Ideally, the reference should get assigned before other code parts try to access it, for example, in Start(). The Awake() method might be a good place to look for an ScoreKeeper object.

Did this help?


See also:

1 Like

I believe Gary made a mistake. You’d usually make the getter GetInstance() static and get it via <ClassName>.GetInstance(), exactly like you did it.

1 Like

That is what I thought, but I was very curious about that detail lol.

Thanks for the reply Nina. I didn’t share the whole code because I was referencing what was being done on the course and didn’t think it was important for the context of the question.

I usually end up making my own solutions because of my coding style, but the thing is that since Gary didn’t complete that part because he wasn’t needing it, he vaguely mentioned how to make it work, but the scenario is a bit different from the AudioPlayer class.

Was I interested in his solution? Yes, because I love to see different approaches on things so I can have them “in my arsenal”. I just wanted to see the way Gary was going to do it, so, sorry, I can’t not worry about his solution because my goal was to see how that would have worked.

I made my solution work because I wanted to move on with the pattern to try out some other things, but that has nothing to do with why I wanted to know how he intended it to work if he went with the pattern.

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

Privacy & Terms