• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tekksin · Apr 21, 2014 at 07:28 AM · playerprefssavebooleanreferenceplayerprefsx

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tekksin · Apr 21, 2014 at 08:16 AM 0
Share

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!

avatar image Burla · Apr 21, 2014 at 08:19 AM 1
Share

@Tekksin - Don't forget to mark it as an answer. Let's keep the community clean! :-)

avatar image Tekksin · Apr 21, 2014 at 08:23 AM 0
Share

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.

Instead 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 (minus 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.

avatar image robertbu · Apr 21, 2014 at 08:45 AM 0
Share

My assumption was that 'LVLnameTimeBadge' was a variable, not an attempt at a string. Glad you got it working.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player prefs and bool 5 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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges