Saving settings?

I’m trying to add an options screen for Laser Defender image
I want only these 2 kind of options but I have some problems. I created a set volume function inside the music player and it works really well with the slider IF I start the game from the options screen. Else it won’t work! I think it’s because of the singleton pattern as the music volume slider won’t have a refference of the music player object. How could I possibly solve this? Also I wish to save this data to the device so when the game starts again the user will have the same settings. (this is less importent at the momment, i’m struggling mainly with the refference thing) Thank you for your help in advance! :slight_smile:

Hi Richard,

At what point do you create the Music Manager instance? Is it only during the Game scene? If so, then this would perhaps explain why you don’t have access to it when you go to the Options scene from the Main Menu, because you’ve not created it yet.

Assuming so, changing where you create this object may work for you, e.g. in the Start scene.

So, after creating the instance it would perhaps check for any saved data. If none exists, use a default value from the MusicManager. If some does exist, use it. So you’d perhaps have a musicVolume variable and a defaultMusicVolume variable.

When the Options scene is loaded, you’d grab the musicVolume variable and use its value to set the slider. When the player changes the value, update the musicVolume variable and save the final value back to the PlayerPrefs when they click a “Save” / “Close” button etc.

For your second query regarding saving the details, take a look at PlayerPrefs, the good news is that this is covered during the Glitch Garden section - so if you’re in a hurry, you could always check the specific lecture. Note, check the “Original” Glitch Garden section of the course, as the remastered version is in production as we speak.

Hope this helps :slight_smile:

See also;

Thank you for fast response!

I don’t have a music manager, but a Music Player and the slider. The music player is instantiated first in the main menu. The music slider has it’s refference to the one that’s instantiated in the options scene. Also I can’t use FindObjectOfType as the music slider directly asks for the script of the instance.image image

Hi Richard,

You’re welcome.

Create yourself an Options.cs for example and attach that to a GameObject in the Options scene. Use that object as the reference on the OnValueChanged event for the Slider component. Have a method in Options.cs that you access for the Slider.

In code with Options.cs you can then use Find ObjectOfType<MusicPlayer>() to return the MusicPlayer instance and access its methods.

Hope this helps :slight_smile:

1 Like

Wow, this solution is way too easy and it didn’t come to my mind haha. Thank you!

1 Like

Hehe, you’re welcome.

You could think of it along these lines, Options.cs is responsible for the interactions between the scene and the relevant systems, your MusicPlayer being on of those systems.

Based on your screenshot you might have a PlayerControls system or similar too. The Options class can talk to both.

Hope this helps :slight_smile:

1 Like

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

Privacy & Terms