It's become pretty apparent that we're going to need a way to keep variables constant between scenes in our game for a variety of purposes. The first things that come to mind are sound/keybind settings, and keeping track of things such as game state such as when the game is paused, and when the player completes the level.
The easiest yet probably least optimised way to do all of this is with PlayerPrefs, but they're intended to be used for variables that need to persist game restarts, and don't need to be accessed frequently, so I'd like to do it the proper way. Or, well, a proper way.
The method I've picked is creating a singleton: a method of accessing a class instance that ensures there is only ever one instance of the class loaded at the same time. To achieve this goal, I'm going to be creating a
Globals class with a static instance of itself, called
Globals.Instance for simplicity. When getting this variable, it will first check if the instance being accessed for this search has already stored. If it hasn't, it will run a Unity call searching for objects of type Globals. If this search was successful, it will store the found object and return it. If it was unable to find an existing instance of Globals (a GameObject named "GlobalsHost"), it will create one, set it to
DontDestroyOnLoad (meaning it will persist between loading different scenes), and return that for the accessor to interface with.
In hindsight, I wish I had made some of the other code in the game more modular so my teammates and I could make more use of the Globals class, for things like more shared code and events.