- Home /
How to Get a PlayerPrefsX boolean?
Hey everybody, I'm not particularly sure how to reference booleans that have and have not been created, yet, through playerPrefsX. I wrote for the boolean to save, but I did it while using variables, to create a unique name. So my boolean script looks like this:
PlayerPrefsX.SetBool(thisLevel + "TimeBadge", true);
That works fine and dandy, but how do I reference it in the current script or others?
For Example: I want to make an if statement that determines whether or not that boolean is true. What would I type? I have fruitlessly tried this:
if(PlayerPrefsX.GetBool(LVLnameTimeBadge){
}
and some other failed experiments. Let me know if you would like to read those.
Also, I believe I need some type of for loop to help the system understand what to look for, because the playerprefsx booleans that don't exist yet (the false ones), can't load the technically false booleans. (makes sense, because they aren't made until the BadgeBool achievement is unlocked)
Links / tips / tricks / etc all appreciated!
Answer by robertbu · Apr 21, 2014 at 07:46 AM
PlayerPrefsX.GetBool() is of the form:
static function GetBool(name : String, defaultValue : boolean) : boolean {
And you are not supplying a default value in the code you posted. So if you want to return false if a variable has not been set yet, then you would do:
if(PlayerPrefsX.GetBool(LVLnameTimeBadge, false)){
So if it finds the key, the boolean value (true or false) is returned. If it does not find the key, then the default value is returned. So there is no need for any kind of loop.
edit: you essentially wrote what I wrote that I tried, and somehow it worked..... I don't know why it's working now, but who cares lol Thanks a lot!
@Tekksin - Don't forget to mark it as an answer. Let's keep the community clean! :-)
Oh, I know why it wasn't working now. We are indeed both wrong here. The boolean in the player prefs needs to be in quotes.
Ins$$anonymous$$d of:
if(PlayerPrefsX.GetBool(LVLnameTimeBadge){
We need:
if(PlayerPrefsX.GetBool("LVLnameTimeBadge"){
It's always the smallest of mistakes.
EDIT: Burla, as you can see in the initial post, robert wrote exactly what I did ($$anonymous$$us the ", false" part, which I always skip in favor of the name with or without an exclamation mark). I'll mark it right so that people going by can read through the extent of this dialogue, but I hope they don't stop reading too soon. This was all just one of my many (many) tiny mistakes in unity script lol.
$$anonymous$$y assumption was that 'LVLnameTimeBadge' was a variable, not an attempt at a string. Glad you got it working.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143213im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player prefs and bool 6 Answers
How to save an array to PlayerPrefs? 1 Answer
For In Loop Fills all values in arrays problem 2 Answers
Saving Array Objects in Android. 0 Answers
Issues with Save / Load system 1 Answer