- Home /
PlayerPrefs Help
Hi there!
I need some help with a script I'm working on. I have a settings menu and there's a button that I can press that turns the background music on or off. I need to save some information by using PlayerPrefs.
I know how to save ints, floats, strings etc. But one problem I have is to save booleans.
When I press the "Sound Off" button this piece of code activates "songObject.audio.volume = 0.0;". The songObject is the object that has the sound file on it.
I've implemented so when you press the button it stays highlighted, thats why I have "soundOn" and "soundOnSelected".
So in some way I need to save if the sound is On or Off. I've tested using the "PlayerPrefsX" script from the Unity wiki but it didn't work unfortunately.
//On
if (soundOn)
{
if (GUI.Button (Rect (Screen.width/2-250, Screen.height - 250, 130, 40), "ON", "button"))
{
soundOn = false;
soundOff = true;
soundOnSelected = true;
soundOffSelected = false;
songObject.audio.volume = 1.0;
AudioSource.PlayClipAtPoint(clickSound, transform.position);
}
}
if (soundOnSelected)
{
if (GUI.Button (Rect (Screen.width/2-250, Screen.height - 250, 130, 40), "ON", "buttonSelected2"))
{
soundOnSelected = true;
soundOffSelected = false;
songObject.audio.volume = 1.0;
AudioSource.PlayClipAtPoint(clickSound, transform.position);
}
}
//Off
if (soundOff)
{
if (GUI.Button (Rect (Screen.width/2-50, Screen.height - 250, 130, 40), "OFF", "button"))
{
soundOn = true;
soundOff = false;
soundOnSelected = false;
soundOffSelected = true;
songObject.audio.volume = 0.0;
AudioSource.PlayClipAtPoint(clickSound, transform.position);
}
}
if (soundOffSelected)
{
if (GUI.Button (Rect (Screen.width/2-50, Screen.height - 250, 130, 40), "OFF", "buttonSelected2"))
{
soundOnSelected = false;
soundOffSelected = true;
songObject.audio.volume = 0.0;
AudioSource.PlayClipAtPoint(clickSound, transform.position);
}
}
Answer by MrSoad · Oct 22, 2014 at 10:51 AM
There is no boolean for player prefs. Create an appropriately name int player pref if you want it to be true(if it does not already exist), destroy this player pref if you want it to be false(if it exists). Then just check for weather it exists in whatever part of the game this matters in.
Setting an int to 0 or 1 will probably be faster then checking if one exists.
Ah I did not know that Bored$$anonymous$$ormon, I'd read about doing it but thought it sounded silly, player prefs are slow anyway so is the difference big or not? If not I will stick with my way... Voted you up for the useful info, thanks :)
Can you give me an example with 0 or 1. $$anonymous$$inda new to this stuff
He means use an int player pref with the value set to 0 for false and 1 for true. Just look at the Unity docs on player prefs if you don't know how to set them up. They are very easy and something you should have no problem with :)
To read a boolean from PlayerPrefs use:
bool isSoundOn = PlayerPrefs.GetInt("soundOn", 1) != 0; // default value of "1" will evaluate to true.
To write a boolean to PlayerPrefs:
PlayerPrefs.SetInt("soundOn", isSoundOn?1:0);
@Landern: Your last line of code doesn't make much sense, you might want to read it again ;)
What imking probably ment is to convert an integer value to a boolean. In this case he should have written something like this:
bool isSoundOff = (someIntValue != 0)?true:false;
However using the ternary operator to return true and false makes no sense since the condition is already a boolean. This would be exactly the same:
bool isSoundOff = someIntValue != 0;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Tutorial level 2 Answers
Pause Menu background problem 0 Answers