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
0
Question by ThatBenGuy · Nov 24, 2012 at 04:54 AM · buttonsavemute

PlayerPrefs for Mute/Unmute button

Hello I'm linking together separate buttons to mute and unmute audio. Here's the mute script: Updated

 var mySkin : GUISkin;
 function OnGUI () {
 GUI.skin = mySkin;
      if (GUI.Button (Rect (10, 550, 65, 35), "Mute")) {
             audio.mute = true;  
             var script1 = GetComponent("UnMuteButton"); 
             script1.enabled = true;
             var script2 = GetComponent("MuteButton"); 
             script2.enabled = false;
             PlayerPrefs.SetInt("AudioIsMuted", 1);
     } 
 }


And here's the unmute script:

 GetComponent("UnMuteButton").enabled = false;
 var mySkin : GUISkin;
 function OnGUI () {
 GUI.skin = mySkin;
      if (GUI.Button (Rect (10, 550, 65, 35), "Unmute")) {
             audio.mute = false;
             var script1 = GetComponent("UnMuteButton"); 
             script1.enabled = false;
             var script2 = GetComponent("MuteButton"); 
             script2.enabled = true;
     } 
 }

When the Mute button is clicked the audio becomes muted and the button is replaced by an Unmute button, and vice versa. What I'd like to know is how can I use PlayerPrefs to save whether or not the Player has muted or unmuted the audio and access their preferences whenever they return? I understand a little about PlayerPrefs but not a whole lot.

Thank you for any answers or feedback, -Ben


Update I've updated the mute code, adding `PlayerPrefs.SetInt("AudioIsMuted", 1);`

I'm using

 function Update(){
 
     var audioIsMuted = PlayerPrefs.GetInt("AudioIsMuted") ==1;
 
 }

To load the player's preference but I still can't get it to work.

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
1

Answer by DannyB · Nov 24, 2012 at 10:01 AM

Use PlayerPrefs.SetInt, like this:

When the user clicks:

 PlayerPrefs.SetInt( "AudioIsMuted", 1 ); // for mute
 // or
 PlayerPrefs.SetInt( "AudioIsMuted", 0 ); // for unmute

When you want to check the status on load:

 bool audioIsMuted = PlayerPrefs.GetInt( "AudioIsMuted" ) == 1;
Comment
Add comment · Show 6 · 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 ThatBenGuy · Nov 25, 2012 at 05:27 AM 0
Share

Thanks for your answer. I incorporated your SetInt code and placed your GetInt code into another script called 'Get$$anonymous$$utePreferences'. The inspector is saying to insert a semicolon at the end of 'boolean'. I did this but then it becomes an error.

avatar image DannyB · Nov 25, 2012 at 09:19 AM 0
Share

Ahm.... what is the line of code and what is the exact error?

avatar image ThatBenGuy · Nov 25, 2012 at 09:38 AM 0
Share

Sorry, should've been more specific. The line of code is: bool audioIs$$anonymous$$uted = PlayerPrefs.GetInt("Audio$$anonymous$$uted") == 1; The console said to insert a semicolon at the end of 'bool' which I did but now it's declaring that 'bool' is an unknown identifier.

avatar image DannyB · Nov 25, 2012 at 10:10 AM 0
Share

Oh - my code is in C#, just use the javascript way of declaring a variable. var audioIs$$anonymous$$uted = ...

avatar image ThatBenGuy · Nov 27, 2012 at 01:12 AM 0
Share

Okay thanks

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.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Temporarily disable gamepad button 1 Answer

Unity - keep created buttons after quit 1 Answer

Find Location via script query 0 Answers

Interactable not save PlayPrefs 1 Answer

Editing an XML File 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