- Home /
Audio plays only once and not playing on click of button again(using audio manager script)
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:
using UnityEngine.Audio; using System; using UnityEngine;
public class AudioManager : MonoBehaviour {
public Sound[] sounds;
// Start is called before the first frame update
void Awake()
{
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
void Start()
{
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)
}
public void Play(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
if (s == null)
{
Debug.LogWarning("Sound: " + name + "not found!");
return;
}
if (PauseMenu.GameIsPaused)
{
s.source.volume = 0.3f;
s.source.pitch = 0.1f;
}
else
{
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
s.source.Play();
}
}
For adjusting the volume and pitch I have a script named sound:
using UnityEngine.Audio; using UnityEngine;
[System.Serializable] public class Sound {
public string name;
public AudioClip clip;
[Range(0f,1f)] //thiis is range in which we want volume(the float is below) to be adjusted
public float volume;
[Range(0.1f,3f)] //this adds slider where we can adjust volume AND pitch
public float pitch;
public bool loop;
[HideInInspector] //this won't show the below variable source in inspector
public AudioSource source;
}
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:
FindObjectOfType().Play("PlayerDeath");
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:
FindObjectOfType().Play("MenuButton");
The entire script is as follows: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class PauseMenu : MonoBehaviour { public static bool GameIsPaused; public GameObject pauseMenuUI; void AndroidBackButtonPause() { FindObjectOfType().Play("MenuButton"); if (Application.platform == RuntimePlatform.Android) { if (Input.GetKey(KeyCode.Escape)) { pauseMenuUI.SetActive(true); Time.timeScale = 0f; GameIsPaused = true; return; } } } // Update is called once per frame public void ButtonClick() { FindObjectOfType().Play("MenuButton"); if (GameIsPaused) { Resume(); } else { Pause(); } } void Resume() { pauseMenuUI.SetActive(false); Time.timeScale = 1f; GameIsPaused = false; } void Pause() { pauseMenuUI.SetActive(true); Time.timeScale = 0f; GameIsPaused = true; } public void ResumeGame() { FindObjectOfType().Play("MenuButton"); pauseMenuUI.SetActive(false); Time.timeScale = 1f; GameIsPaused = false; } public void GoToMainMenu() { FindObjectOfType().Play("MenuButton"); SceneManager.LoadScene(0); //0 is build settings index for main menu(I have put it in build that way) pauseMenuUI.SetActive(false); Time.timeScale = 1f; GameIsPaused = false; } public void QuitGame() { FindObjectOfType().Play("MenuButton"); Application.Quit(); } }
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 :-( )
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
C# problem with pause menu 2 Answers
Why the audio sound effects of the main menu are not hearing like in mute ? 0 Answers
Audio Clip Playing every frame 2 Answers
audio doesn't play on movement 0 Answers