• 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 /
  • Help Room /
avatar image
0
Question by FailureToAdapt · Jan 03, 2017 at 07:08 PM · unity 5playerprefstogglemutepersistent

Mute Toggle Not Staying On

I have been trying to figure this out for a week now. A this point I am completely at a loss.

What I am trying to do is have a UI Toggle act as the mute button. I have a main menu, a main level and a death screen. When I click the toggle on the main menu and continue to the next scene (the main level) the game stays muted, but the check mark is no longer present in the toggle and I have to click the mute button once to get the toggle back and then click it again to unmute the game. The same issue is happening upon entering the death menu. The death menu restarts the level and ends up unchecking the the UI toggle when it resets.

I have tried saving to player prefs. I have tried DontDestroyOnLoad.... I have tried MANY things. All I want is a mute toggle that persists through scenes and through application quit.

Here is the code I currently have. At this point I have modified it so many times I am not sure if this code is working or not.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class MuteToggle : MonoBehaviour {
 
     bool isMute;
 
     void Start()
     {
     PlayerPrefs.GetInt ("MuteToggle");
     }
     // Use this for initialization
     public void MuteButton ()
 
     {
         isMute = ! isMute;
         AudioListener.volume =  isMute ? 0 : 1;
         PlayerPrefs.SetInt ("MuteToggle", 1);
         PlayerPrefs.Save();
 
 
     }
 }
 

At this point I would do anything for someone to walk me through this.

Comment
Add comment · Show 3
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 hexagonius · Jan 03, 2017 at 08:11 PM 0
Share

in Start your getting the value but you don't save it, probably should read:

 is$$anonymous$$ute = PlayerPrefs.GetInt...
avatar image FailureToAdapt hexagonius · Jan 03, 2017 at 08:13 PM 0
Share

I will try this again, but I am pretty sure it says I can't do that because is$$anonymous$$ute is a bool. I am not in front of my computer at the moment, but I will be in about an hour and I will plug that in and try it.

avatar image FailureToAdapt hexagonius · Jan 03, 2017 at 11:35 PM 0
Share

I updated my script, but it is giving me the bool error I mentioned earlier

2 Replies

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

Answer by FailureToAdapt · Jan 04, 2017 at 08:51 PM

Eventually I found the answer I was looking for through a tutorial on Youtube. This One

I ended up attaching this code to my main menu script:

 public void ToggleSound()
     {
         if (PlayerPrefs.GetInt ("Muted", 0) == 0) 
         {
             PlayerPrefs.SetInt ("Muted", 1);
         } else {
             PlayerPrefs.SetInt ("Muted", 0);
         }
 
         SetSoundState();
 
     }
 
     private void SetSoundState()
     {
         if (PlayerPrefs.GetInt ("Muted", 0) == 0) {
             AudioListener.volume = 1;
             audioOnIcon.SetActive (true);
             audioOffIcon.SetActive (false);
         } else {
             AudioListener.volume = 0;
             audioOnIcon.SetActive (false);
             audioOffIcon.SetActive (true);
         }
     }

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

Answer by Kiefy · Jan 03, 2017 at 08:16 PM

So you're reading the PlayerPrefs value for MuteToggle but it doesn't appear that you're applying it.

I'm pretty new to this myself, but try adding in Start():

 // in case there's a bool<->int conversion issue
 isMute = PlayerPrefs.GetInt ("MuteToggle") ? true : false;  
 AudioListener.volume = isMute ? 0 : 1;

and when saving:

 PlayerPrefs.SetInt ("MuteToggle", isMute ? 1 : 0;
 PlayerPrefs.Save();
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 FailureToAdapt · Jan 03, 2017 at 11:35 PM 0
Share

Just tried that. I am still getting a bool error.

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

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

PlayerPrefs.SetString creates a REG_BINARY instead of REG_SZ inside the registry 0 Answers

How to save the state of Canvases/Toggles? 1 Answer

how to stop Playing Audio in Required Scene 1 Answer

save one big json string vs multi seperate json strings in playerprefs 0 Answers

How I can mute music by toggle !! 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