Yes, that seems to be the problem. However, this is not supposed to happen.
Unless something destroys the slider when the scene got reloaded, the concerning field should still reference the slider. Alternatively, it might be that your code overrides the value of the field with null
. That’s something you’ll have to analyse further. If nothing helps, disable all game objects but the slider and maybe the game object which loaded the next scene. Then test your game again. If the problem disappeared, enable the disabled game object step by step and test all of them until the problem reappears. Debugging is not difficult, just repetitive and boring from time to time.
Regarding your three approaches, do you need the slider to be in each scene? If so, making it persistent via DontDestroyOnLoad makes sense. If not, I would not make it persistent.
Why would you want to reference the slider anywhere? Referencing is always a bit tricky because a lot is going on during runtime, and we don’t have much control over Unity.
If you want to challenge yourself, create a VolumeHandler class and attach it to the same game object as your Slider component. You could either add the reference to the Slider manually or via GetComponent. The VolumeHandler “communicates” with the PlayerPrefsController.
AudioSources get the relevant data from the PlayerPrefsController, not from the slider or the VolumeHandler.
Instead of a VolumeHandler, you could even create a more general SliderHandler class or something like that and work with enums which are mapped to strings in the PlayerPrefsController class. Look enums up on DotNetPerls to see an example. You could define different slider types and create an enum. If you have multiple sliders and different types, this approach could make sense so you don’t have to create 4 different classes and duplicate code 4 times in the PlayerPrefsController.