• 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
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

People who like this

0 Show 0
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
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
Tekksin
Burla

People who like this

2 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Issues with Save / Load system 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges