• 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 Uping · Sep 15, 2014 at 10:09 AM · soundmusicvolumemute

Mute volume / sound problem

okay my problem is when i press the mute button it works fine and unmute is fine but the problem is ive set it with a picture button but what happens is when I mute it and go to the next scene and go back again the volume is still muted but the button for it is for not muted okay here's my code

 void OnGUI(){
 if(yMute){
             if (GUI.Button(new Rect(100, 10, 100, 50), "",Y))
             {
                 AudioListener.volume = 0.0F;
                 muted = true;
                 nMute = true;
                 yMute = false;
             }
         }
         if(nMute){
             if (GUI.Button(new Rect(100, 10, 100, 50), "",N))
             {
                 AudioListener.volume = 1.0F;
                 muted = false;
                 nMute = false;
                 yMute = true;
             }
         }
     
     }
 
     void Start () {
         yMute = true;
 
     }
     
     // Update is called once per frame
     void Update () {
         if (yMute == true) {
             nMute = false;
         }
         else {
             nMute = true;
         }
     }
 }



so what I need is when i press my mute button the picture will change for the unmute and when i press it again the unmute will change for the mute picture it works fine but when i go to the next scene and go back here again the volume is muted but my button is for not muted so how can i fix that ? please help me thank you

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
Best Answer

Answer by VincentDB · Sep 15, 2014 at 11:06 AM

The problem is that your using multipe variables to detect if user have alredy muted or not the sound. And you reset to false yMute variable at Start, so when you back to the scene, yMute is set to true, but nMute keep his old value.

Use only one variable would be better, something like:

 void OnGUI()
 {
     if(!isMuted)
     {
         if (GUI.Button(new Rect(100, 10, 100, 50), MuteImage))
         {
             isMuted = true;
             AudioListener.volume = 0.0F;
         }
     }
     else
     {
         if (GUI.Button(new Rect(100, 10, 100, 50), unMuteImage))
         {
             isMuted = false;
             AudioListener.volume = 1.0F;
         }
     }
 }
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 Uping · Sep 15, 2014 at 11:22 AM 0
Share

thankyou but, still wont work it wont even make the volume go to 0 i dont know why and when I press it its good the button is changing but when i go to the next scene with the un$$anonymous$$uteImage and go back its already changed to $$anonymous$$uteImage.....

avatar image VincentDB · Sep 15, 2014 at 11:43 AM 1
Share

Try to save a value.

 void OnGUI()
 {
 
     if(PlayerPrefs.GetInt("Is$$anonymous$$uted") == 0)
     {
         if (GUI.Button(new Rect(100, 10, 100, 50), $$anonymous$$uteImage))
         {
             PlayerPrefs.SetInt("Is$$anonymous$$uted", 1);
             AudioListener.volume = 0.0F;
         }
     }
     else
     {
         if (GUI.Button(new Rect(100, 10, 100, 50), un$$anonymous$$uteImage))
         {
             PlayerPrefs.SetInt("Is$$anonymous$$uted", 0);
             AudioListener.volume = 1.0F;
         }
     }
 }
avatar image Uping · Sep 15, 2014 at 12:33 PM 0
Share

worked like a charm thank you so much made my life easier :)

avatar image Uping · Sep 15, 2014 at 12:39 PM 0
Share

the only problem now is when the game restarts it goes back with sound

avatar image VincentDB · Sep 15, 2014 at 12:50 PM 1
Share

If you want keep player settings for next restart game, you have to save this setting by:

 PlayerPrefs.Save();

You can use this function by only one call on the function:

void OnApplicationQuit() { PlayerPrefs.Save(); }

Np and good continuation !

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How To Turn Off Sound For Certain Objects? 1 Answer

Why is my backtrack's volume getting lower? 0 Answers

Low sound volume on phone speaker but normal on headphone 0 Answers

Mute unity app if another external app is playing music in the background on iOS 0 Answers

Unity and music tracks - load track at certain point and fading tracks in and out 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges