• 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 Entyro · Oct 22, 2014 at 10:21 AM · javascriptplayerprefs

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);
     }
 }
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
0
Best Answer

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.

Comment
Add comment · Show 8 · 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 Kiwasi · Oct 22, 2014 at 10:57 AM 1
Share

Setting an int to 0 or 1 will probably be faster then checking if one exists.

avatar image MrSoad · Oct 22, 2014 at 11:11 AM 0
Share

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 :)

avatar image Entyro · Oct 22, 2014 at 11:38 AM 0
Share

Can you give me an example with 0 or 1. $$anonymous$$inda new to this stuff

avatar image MrSoad · Oct 22, 2014 at 11:42 AM 0
Share

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 :)

avatar image Bunny83 · Oct 22, 2014 at 02:39 PM 2
Share

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; 
Show more comments

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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