A bit confused on ScoreKeeper singleton

I came across a bug where, my score in “Game” scene can only accumulate once, this happens when I load the “Game” scene from the “MainMenu” Scene after I press the “Start” button to play. But the score accumulate normally when I played it right off from the “Game” scene, and not when I click on the “Start” button on the MainMenu.

I fixed the bug by deleting all ScoreKeeper gameObjects from other scenes except for in the “MainMenu” scene. Only then the score accumulate as it should no matter where the “Game” scene is loaded from.

My question is, when making a singleton for a gameObject in the hierarchy, do we need to put it in a single Scene only ? and let it persist through every scene from then on ?

Im a bit confused since, we can have an AudioPlayer singleton in every scene in the Hierarchy without any bug.

Hi Zopdas,

That’s the idea of a singleton. It is supposed to be the only object. And in most cases, it is also supposed to persist in other scenes.

However, it might be that there are additional objects. For example, because we made a mistake or because we reload the scene where our “only” object was created. In that case, the “singleton” is supposed to destroy the other objects.

If your “singleton” does not work properly, check if your “singleton” code calls gameObject.SetActive(false); in the same if-block where Destroy(gameObject); gets called. If there isn’t that line of code, add it.

1 Like

OMG I’ve totally missed that one line of code “gameObject.SetActive(false)”.
I tried to make the ScoreKeeper singleton myself without referring to the AudioPlayer singleton. :sweat_smile:

I’ve added it and It works fine now ! Thanks Nina !

I’m glad the solution was so simple. :slight_smile:

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

Privacy & Terms