Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved Audio Not Playing at the Beginning of Scene

Discussion in 'Audio & Video' started by EddieFromIndia, May 27, 2020.

  1. EddieFromIndia

    EddieFromIndia

    Joined:
    May 2, 2020
    Posts:
    11
    Hi,
    I've added a Sound Toggle option at the settings of the game. When the sound is toggled ON or OFF, the value 1 or 0 is stored in the PlayerPrefs. While opening the game scene, the Start method checks the value of the PlayerPrefs and sets the AudioListener.volume to 0 or 1. But, when I start the game for the first time, the sound is muted. When I go back to the Menu again, and come back to game, the sound plays correctly.
    While the game is running for the first time, I added a Debug.Log to check the AudioListener.volume and AudioListener.pause and they were 1 and false respectively, which means that the sound should play, but it isn't.
    Also, I should mention that the game has a feature in the Menu, where I can clear all PlayerPrefs and set some of them to default values, which includes the sound state being set to 1 (means ON). The problem even re-appears during the first gameplay after a PlayerPrefs reset.
    I don't know what's wrong with my code or anything that prevents the audio being played when the game is run for the first time, or after a PlayerPrefs reset.

    Note: there is only one AudioListener in the scene, and the sounds work correctly from the second gameplay.

    This is in the game scene script:
    Code (CSharp):
    1. void Start()
    2. {
    3.     //Turn On-Off Sound
    4.     if (PlayerPrefs.GetInt("SoundState") == 0)
    5.     {
    6.         AudioListener.volume = 0.0f;
    7.         Debug.Log(AudioListener.volume);
    8.         Debug.Log(AudioListener.pause);
    9.     }
    10.     else
    11.     {
    12.         AudioListener.volume = 1.0f;
    13.         Debug.Log(AudioListener.volume);
    14.         Debug.Log(AudioListener.pause);
    15.     }
    16. }
    This is in the Menu script:
    Code (CSharp):
    1. void Start()
    2. {
    3.     InitializePrefs();
    4.  
    5.     //Turn On-Off Sound
    6.     if(PlayerPrefs.GetInt("SoundState") == 0)
    7.     {
    8.         AudioListener.volume = 0.0f;
    9.     }
    10.     else
    11.     {
    12.         AudioListener.volume = 1.0f;
    13.     }
    14. }
    15. public void ResetPrefs()
    16. {
    17.     PlayerPrefs.DeleteAll();
    18.     InitializePrefs();
    19. }
    20. void InitializePrefs()
    21. {
    22.     if (!PlayerPrefs.HasKey("SoundState"))
    23.     {
    24.         PlayerPrefs.SetInt("SoundState", 1);
    25.     }
    26. }
     
  2. EddieFromIndia

    EddieFromIndia

    Joined:
    May 2, 2020
    Posts:
    11
    It was just my fault. I placed the Debug.Log in the Start. When I shifted it to Update, I found out that the volume gets turned off at a point because I have added a AudioListener.volume = 0.0f; at some point later.
    My mistake.