Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by Glowy · Apr 07, 2011 at 07:28 PM · playerprefssaveloadboolean

Player prefs and bool

I'm having a hard time figuring out how to use player prefs to save and load the game(automatic at game start) in c#.

It doesn't help that most of the things that should be saved are of the bool type, and it only seems to take int and strings, do I really have to convert all my bools if so, what would be the cleanest way to do that?

So how would one go saving and loading bool types?

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

6 Replies

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

Answer by Eric5h5 · Apr 07, 2011 at 08:42 PM

You can use BoolPrefs (still uses GetInt/SetInt, but you don't have to think about it).

Comment
Add comment · Show 2 · 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 Nick4 · Jul 23, 2014 at 10:01 AM 2
Share

Link is long gone but this is what it probably looks like if anyone's wondering:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerPrefs2
 {
     public static void SetBool(string key, bool state)
     {
         PlayerPrefs.SetInt(key, state ? 1 : 0);
     }
 
     public static bool GetBool(string key)
     {
         int value = PlayerPrefs.GetInt(key);
 
         if (value == 1)
         {
             return true;
         }
 
         else
         {
             return false;
         }
     }
 }
avatar image Eric5h5 · Jul 29, 2014 at 09:02 PM 1
Share

The page is at http://wiki.unity3d.com/index.php/BoolPrefs now. The actual script is much shorter...the GetBool function can easily be done in 1 line. ;)

avatar image
1

Answer by CryptoGrounds · Jun 13, 2019 at 11:21 PM

Just convert to string:

 bool tempbool = true;
 PlayerPrefs.SetString("testBool", tempbool.ToString());
 tempbool = Boolean.Parse(PlayerPrefs.GetString("testBool", "true"));
Comment
Add comment · Show 1 · 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 technorocker · Jan 30, 2020 at 02:18 AM 0
Share

This didn't work for me. The get string part where you say true just makes it true. I have a bool that can change depending on user input.

avatar image
0

Answer by letroll · Oct 02, 2015 at 09:13 AM

Or you can create this extension:

 using UnityEngine;
 using System.Collections;
 
 public static class PlayerPrefExtension
 {
     public static bool GetBool (this PlayerPrefs playerPrefs, string key)
     {
         return PlayerPrefs.GetInt (key) == 1;
     }
 
     public static void SetBool (this PlayerPrefs playerPrefs, string key, bool state)
     {
         PlayerPrefs.SetInt (key, state ? 1 : 0);
     }
 }
Comment
Add comment · Show 1 · 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 Novack · Mar 19, 2016 at 03:46 PM 0
Share

If I recall correctly, extension methods require an instance of an object: will not work on static classes, so that is not possible.

avatar image
0

Answer by Ricna · Jul 30, 2014 at 03:18 AM

Bool is nothing more than yes or no, true or false, something or zero.. So, considering a simple C program.. I usually use 2 methods to access 1 attribute.

Example:

 public class User{
 
     private string FIELD_PREMIUM = "premiumAccount";
     private bool premiumAccount;
 
     public User(){
         this.LoadUser();
     }
 
     public void LoadUser(){
         this.premiumAccount = PlayerPrefs.GetInt(FIELD_PREMIUM,0)>0?true:false;
     }
 
     public void PremiumAccountPurchased(){
         this.premiumAccount = true;
         this.Save();
     }
 
     public bool HasPremiumAccount(){
         this.LoadUser();
         return this.premiumAccount;
     }
 
     public void Save(){
         PlayerPrefs.SetInt(FIELD_PREMIUM, this.premiumAccount?1:0);
     }
 }
 


All that you need is:

To get the value: bool value = PlayerPrefs.GetInt("somefield",0)>0?true:false;

To set the value: PlayerPrefs.SetInt("somefield", myBoolValue?1:0);

I hope this helps your or someone :)

Comment
Add comment · Show 1 · 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 technorocker · Jan 30, 2020 at 02:24 AM 0
Share

Thank you for your reply. this is the one that worked for me out of the couple ways I tried from other people that replied to this post.

avatar image
0

Answer by MianAbdul · Mar 21, 2018 at 02:54 PM

"Get" by using this line: bool myBool = PlayerPrefs.GetInt ("VariableName") == 1 ? true : false;

"Set" by using this line: PlayerPrefs.SetInt ("VariableName", true ? 1 : 0);

Comment
Add comment · 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
  • 1
  • 2
  • ›

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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

C# Issues with either PlayerPrefs.SetVariable or UnityEvent.Invoke 1 Answer

How to store a boolean for each variable in a class 1 Answer

There are wrong mathematical equations in PlayerPrefs. How can I resolve it? 1 Answer

Highscore won't save 3 Answers

how to save a load a game? 1 Answer


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