Erfan
Erfan

Reputation: 3353

how can mute current scene for ever in unity c#

have Toggle on/off for mute volume and this is my function :

public    void Mm(bool vv)
     {
         if (vv)
         {
             AudioListener.volume = 0;
         }
         else 
         {
             AudioListener.volume = 1;
         }
     }

all thing work well but problem is : when i mute and go another scene , when come back in this scene again volume not mute!

Upvotes: 1

Views: 212

Answers (1)

Umair M
Umair M

Reputation: 10720

Create a SoundManager script like below and place it in your current scene :

public class SoundManager : MonoBehaviour
{
    void OnEnable()
    {
        AudioListener.volume = PlayerPrefs.GetFloat("volume",0);
    }

    void OnDisable()
    {
        AudioListener.volume = 1;
    }

    public void Mute(bool vv)
    {
        if (vv)
        {
            AudioListener.volume = 0;
        }
        else 
        {
            AudioListener.volume = 1;
        }
        PlayerPrefs.SetFloat("volume",AudioListener.volume);
        PlayerPrefs.Save()
    }
}

Upvotes: 2

Related Questions