Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Audio not playing on click of button(using audio manager script)

Discussion in 'Scripting' started by phantom_x404, May 3, 2020.

  1. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    Hello, I have made an audio manager script to be used in my game, the script is simple, it is added to an audio manager object in the scene where I want to use it, it requires: number of audio tracks in scene, name of track, an option to loop it, the audio track itself, and we can adjust the pitch and volume of the audio track from the audio manager itself.
    The code for audio manger is given below:
    Code (CSharp):
    1. using UnityEngine.Audio;
    2. using System;
    3. using UnityEngine;
    4.  
    5. public class AudioManager : MonoBehaviour
    6. {
    7.  
    8.     public Sound[] sounds;
    9.  
    10.     // Start is called before the first frame update
    11.     void Awake()
    12.     {
    13.         foreach (Sound s in sounds)
    14.         {
    15.             s.source = gameObject.AddComponent<AudioSource>();
    16.             s.source.clip = s.clip;
    17.  
    18.             s.source.volume = s.volume;
    19.             s.source.pitch = s.pitch;
    20.             s.source.loop = s.loop;
    21.         }
    22.     }
    23.  
    24.     void Start()
    25.     {
    26.         Play("InGame"); //this plays the InGame audio file whenever the scene starts, and stops after scene changes(It would be awesome if anyone could tell me another way for doing this on the exact time the player ies)
    27.     }
    28.  
    29.     public void Play(string name)
    30.     {
    31.         Sound s = Array.Find(sounds, sound => sound.name == name);
    32.         if (s == null)
    33.         {
    34.             Debug.LogWarning("Sound: " + name + "not found!");
    35.             return;
    36.         }          
    37.  
    38.         if (PauseMenu.GameIsPaused)
    39.         {
    40.             s.source.volume = 0.3f;
    41.             s.source.pitch = 0.1f;
    42.         }
    43.         else
    44.         {
    45.             s.source.volume = s.volume;
    46.             s.source.pitch = s.pitch;
    47.         }
    48.  
    49.         s.source.Play();
    50.     }
    51.  
    52. }
    53.  
    For adjusting the volume and pitch I have a script named sound:
    Code (CSharp):
    1. using UnityEngine.Audio;
    2. using UnityEngine;
    3.  
    4.  
    5. [System.Serializable]
    6. public class Sound
    7. {
    8.  
    9.     public string name;
    10.  
    11.     public AudioClip clip;
    12.  
    13.     [Range(0f,1f)] //thiis is range in which we want volume(the float is below) to be adjusted
    14.     public float volume;
    15.     [Range(0.1f,3f)] //this adds slider where we can adjust volume AND pitch
    16.     public float pitch;
    17.  
    18.     public bool loop;
    19.  
    20.     [HideInInspector] //this won't show the below variable source in inspector
    21.     public AudioSource source;
    22.  
    23. }
    24.  
    Now, I have added three audio files namely:
    i) PlayerDeath - I want to play this when player dies
    ii) InGame - the audio file that plays whenever the scene starts(though if there is a way to end this sound as soon as player collides it would be awesome)
    iii) MenuButton - to be played whenever a UI element button is clicked

    Now, the InGame audio plays from audio manager script itself whenever the scene starts, player death has been added to on collision script, I have a player movement script which includes onCollision, in the on collision part I have added:
    Code (CSharp):
    1.  
    2. FindObjectOfType<AudioManager>().Play("PlayerDeath");
    3.  
    So now whenever the player collides, the script finds the audio with the name PlayerDeath and plays it.
    Similiarily, I have a pause menu UI in the same scene as the Level scene, I want the MenuButton audio to play whenever the pause button is clicked or any of the UI elements for the pause button are clicked, for this I have added the code:
    Code (CSharp):
    1.  
    2. FindObjectOfType<AudioManager>().Play("MenuButton");
    3.  
    The entire script is as follows:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. public class PauseMenu : MonoBehaviour
    7. {
    8.     public static bool GameIsPaused;
    9.     public GameObject pauseMenuUI;
    10.     void AndroidBackButtonPause()
    11.     {
    12.         FindObjectOfType<AudioManager>().Play("MenuButton");
    13.         if (Application.platform == RuntimePlatform.Android)
    14.         {
    15.             if (Input.GetKey(KeyCode.Escape))
    16.             {
    17.                 pauseMenuUI.SetActive(true);
    18.                 Time.timeScale = 0f;
    19.                 GameIsPaused = true;
    20.                 return;
    21.             }
    22.         }
    23.     }
    24.     // Update is called once per frame
    25.     public void ButtonClick()
    26.     {
    27.         FindObjectOfType<AudioManager>().Play("MenuButton");
    28.         if (GameIsPaused)
    29.         {
    30.             Resume();
    31.         }
    32.         else
    33.         {
    34.             Pause();
    35.         }
    36.     }
    37.     void Resume()
    38.     {
    39.         pauseMenuUI.SetActive(false);
    40.         Time.timeScale = 1f;
    41.         GameIsPaused = false;
    42.     }
    43.     void Pause()
    44.     {
    45.         pauseMenuUI.SetActive(true);
    46.         Time.timeScale = 0f;
    47.         GameIsPaused = true;
    48.     }
    49.     public void ResumeGame()
    50.     {
    51.         FindObjectOfType<AudioManager>().Play("MenuButton");
    52.         pauseMenuUI.SetActive(false);
    53.         Time.timeScale = 1f;
    54.         GameIsPaused = false;
    55.     }
    56.     public void GoToMainMenu()
    57.     {
    58.         FindObjectOfType<AudioManager>().Play("MenuButton");
    59.         SceneManager.LoadScene(0); //0 is build settings index for main menu(I have put it in build that way)
    60.         pauseMenuUI.SetActive(false);
    61.         Time.timeScale = 1f;
    62.         GameIsPaused = false;
    63.     }
    64.     public void QuitGame()
    65.     {
    66.         FindObjectOfType<AudioManager>().Play("MenuButton");
    67.         Application.Quit();
    68.     }
    69. }
    70.  
    Now, in the UI for pause menu, I have added an OnClick property to all UI buttons, and their specific function to be executed is mentioned inside the PauseMenu script. So for example: on clicking the resume button, it should execute the ResumeGame function, this works fine whenever the pause menu UI button is clicked when game is being played, but then, when game is paused, and I click on resume button or main menu or any of the others, the sound does not play again. i.e. It only plays one time and not after that, even though the GetAudio component has been added to all events like Resume, Quit, Main menu, etc.
    SO I have two questions:
    i) What may be the best solution to the audio not playing whenever the Resume or Main Menu is clicked after having paused the game?
    ii) Whenever I pause the game I want the audio volume to be reduced, but this does not happen so and the volume and pitch remains the same(I have the code added in the AudioManager Script)
    (please keep in mind I am only a beginner :-( )
     
    Last edited: May 3, 2020
  2. kosted

    kosted

    Joined:
    Mar 14, 2015
    Posts:
    104
    Did you find a solution ? Your script is more or less the same than what i have.
    Once deployed i don't have the sound. script play is not called
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,720
    As with all data/setup "this isn't working" problems, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.