Search Unity

Audio audio isn't stopping even though the debug log is activated

Discussion in 'Audio & Video' started by puppyinasombrero, Mar 4, 2019.

  1. puppyinasombrero

    puppyinasombrero

    Joined:
    Mar 4, 2019
    Posts:
    1
    so i'm making this game where the player is a cube dodging obstacles coming at them. the obstacles have a sound that constantly plays on loop, so when they pass it sounds like they're whizzing by the player. the problem is if you pause and you're right next to an obstacle the audio still plays. i set up code on the obstacles that deactivates their sound when the game is paused, but for some reason it's just not activating. i put a debug log statement right after it and it activates. i tested the command i was using to stop the audio, but it works, just not in my void statement. here's my code:
    Code (CSharp):
    1. public class stopSound : MonoBehaviour
    2. {
    3.     public AudioSource windSource;
    4.     public AudioClip wind;
    5.     public bool isPaused;
    6.     public bool activateSound;
    7.  
    8.     public void silence () // activated from script that handles the pause menu
    9.     {
    10.         isPaused = true;
    11.         Debug.Log("silence() activated");
    12.    
    13.     }
    14.  
    15.  
    16.    public  void playSound () // activated from continue button on pause menu
    17.     {
    18.         isPaused = false;
    19.         activateSound = true;
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         if (isPaused == true) // if the game is paused, stop sound and say "stopped the sound"
    25.         {
    26.             windSource.Stop();
    27.             Debug.Log("stopped the sound");
    28.         }
    29.  
    30.         if (isPaused == false && activateSound == true) // if game isn't paused and the
    31.                                          // wind sound hasn't just been activated then
    32.         {                                      
    33.                                                      
    34.             windSource.Play();                  //  play the sound    
    35.             activateSound = false;              // stop sound from playing again
    36.             Debug.Log("it played the sound");   // and say "it played the sound"
    37.         }
    38.     }
    39.  
    40.  
    41. }
    42.  
     
  2. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    Why not make a single function to toggle the sound on and off? You can call it from the other script whenever the game is paused/resumed and just pass a variable into this function stating whether sound should play or not. I don't see why you need multiple booleans to check whether sound should play or not, either the game is paused (audio shouldn't play) or it is playing and sound should play. I would also avoid using the Update function because there's really no reason you have to check every frame whether the sound should play or not. Just call a ToggleSound function when the user presses the pause/resume button and call windSource.Stop() or windsource.Play() depending on the game state.