• 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 Rebekah-D-David · Jun 06, 2020 at 09:22 PM · toggle button

Sound effects also turned off when switching off music

My toggle button for music is working well. However, it turns off sound effects as well. I want to toggle for both music and sound effects separately. I don't understand where it's gone wrong. Here's my code for the music toggle:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class MusicToggle : MonoBehaviour
 {
     private Music music;
     public Button musicToggleButton;
     public Sprite musicOnSprite;
     public Sprite musicOffSprite;
 
     void Start()
     {
         music = GameObject.FindObjectOfType<Music>();
         UpdateIcon();
     }
 
     public void PauseMusic()
     {
         music.ToggleMusic();
         UpdateIcon();
     }
 
  
     void UpdateIcon()
     {
         if (PlayerPrefs.GetInt("Muted", 0) == 0)
         {
             AudioListener.volume = 1;
             musicToggleButton.GetComponent<Image>().sprite = musicOnSprite;
         }
         else
         {
             AudioListener.volume = 0;
             musicToggleButton.GetComponent<Image>().sprite = musicOffSprite;
         }
     }
 }
     

  
  
Comment
Add comment · Show 1
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 Rebekah-D-David · Jun 06, 2020 at 09:23 PM 0
Share
    using System.Collections;
         using System.Collections.Generic;
         using UnityEngine;
         
         public class $$anonymous$$usic : $$anonymous$$onoBehaviour
         {
             static $$anonymous$$usic Instance = null;
             private void Awake()
             {
                 if (Instance != null)
                 {
                     Destroy(gameObject);
                 }
                 else
                 {
                     Instance = this;
                     GameObject.DontDestroyOnLoad(this);
                 }
             }
         
             public void Toggle$$anonymous$$usic()
             {
                 if(PlayerPrefs.GetInt("$$anonymous$$uted", 0) == 0)
                 {
                     PlayerPrefs.SetInt("$$anonymous$$uted", 1);
                     //AudioListener.volume = 1;
                 }
                 else
                 {
                     PlayerPrefs.SetInt("$$anonymous$$uted", 0);
                     //AudioListener.volume = 0;
                 }
             }
         }
         
 
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UnderShad · Jun 07, 2020 at 12:50 AM

The problem is the AudioListener.volume = 0; AudioListener is like a ear and if you turn ears to zero then no sound will be heard. Use AudioSource.volume=0; Instead

Comment
Add comment · Show 3 · 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 Rebekah-D-David · Jun 07, 2020 at 04:00 AM 0
Share

$$anonymous$$any thanks. Will try this.

avatar image Rebekah-D-David · Jun 07, 2020 at 05:12 AM 0
Share

@UnderShad I got this error now. How can i fix this ?

Error CS0120 An object reference is required for the non-static field, method, or property 'AudioSource.volume'

avatar image Rebekah-D-David · Jun 07, 2020 at 07:38 AM 0
Share

And another thing is that, my playerpref for the toggle value can be loaded upon launching the application. However, since I place my toggle button on the settings panel in the main menu. It is only loaded when i click on the settings button everytime i enter the application.

alt text

capture.jpg (41.2 kB)

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

127 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

Related Questions

Simple toggle camera button 1 Answer

AudioMixer Mute Button 1 Answer

Using save options to create saves for toggle buttons, but won't compile! 1 Answer

How to prevent Unity4.6 Toggle button loosing its state when jumping between scenes? 3 Answers

Sprite Swap Toggle Button not working 0 Answers

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