An even easier way to solve that without moving code around, and keeping the default volume neatly in the OptionsController would be to just create one in the method in PlayerPrefsController itself.
like this:
var settings = new SettingsController();
return settings.GetDefaultVolume();
We already know the game is gonna start with the splash screen, meaning that the first time that GetMasterVolume() is called (when setting the volume in the Music Player on the splash screen), there’s not going to be an OptionsController anywhere.
And this way we never have to worry about whether or not there is one in the scene.
The default volume is also a constant value that the user can’t change, so we don’t have to worry about modifying it on any game objects.
The way my GetMasterVolume method looks like would be:
public static float GetMasterVolume() {
float volume;
if (PlayerPrefs.HasKey(MASTER_VOLUME_KEY)) {
volume = PlayerPrefs.GetFloat(MASTER_VOLUME_KEY);
} else {
var settings = new SettingsController();
volume = settings.getDefaultVolume();
}
return volume;
}
It will throw a warning that you can’t create a MonoBehaviour using the new keyword, however, it’ll still retrieve the value
Edit:
The latter can also be worked around by simply making the default volume and default difficulty static, and then making the get method for them static too. That allows to just call it directly.
[SerializeField] static float defaultVolume = 0.7f;
public static float getDefaultVolume() {
return defaultVolume;
}
and then call it as just:
volume = SettingsController.getDefaultVolume();