Since our UI is composed of GameObjects, they are destroyed on a new scene load, thus we lose our menu as we enter the Game
scene when the Play
button is clicked.
We'll use the singleton method to store data, which will be used regularly, like the score, lives, and so on, and to ensure important UI elements persist from one scene to another. The singleton will also help us to easily access these variables during runtime.
The singleton pattern restricts the instantiation of a class to one unique object. It is useful when one object is required to have some generic actions available throughout different scenes.
With Unity, a singleton is a class that can only be instantiated once: it's attached to a GameObject in the scene that isn't destroyed on scene changing using the DontDestroyOnLoad()
property. This means that the GameObject and all its attached components or children, are persistent through scene changing. You might destroy it manually through code if required,...