- Home /
Menu Play button sound isnt working
Hi. I made a pong game for practice. I added a audio source to the ball to play during collisions and its working. I also added audio source to my MainMenu(empty object) which need to play when I click a UI button. I have Play and Quit button. In Unity Editor, in Game mode, when I press the Quit button, the sound is playing. Play button also has same setup like Quit button, but when I click it, no sound is playing. It successfully loads the Game scene though. So I am guessing that the Game scene loads faster than the sound could play. This is the code for the MainMenu script,
public class MainMenu : MonoBehaviour
{
private AudioSource clickSound;
void Start()
{
clickSound = GetComponent<AudioSource>();
}
public void PlayGame()
{
clickSound.Play();
SceneManager.LoadScene("Game");
}
public void QuitGame()
{
clickSound.Play();
Application.Quit();
}
}
I call the PlayGame() and QuitGame() methods in OnClick settings of the Play button and Quit button respectively. Since its just for practice, I moved on to next practice game. But I need to learn how to do it so I can do it in the next game. Thanks in advance.
Answer by Kakkapylly · Nov 30, 2021 at 08:24 PM
Try to add Animator.Play in "OnClick Settings". I doubt it will work but give it a try
I think this will work 100% public class MainMenu : MonoBehaviour { private AudioSource clickSound;
void Start()
{
clickSound = GetComponent<AudioSource>();
}
public void PlayGame()
{
clickSound.Play();
StartCoroutine(Wait());
}
public void QuitGame()
{
clickSound.Play();
Application.Quit();
}
IEnumerator Wait()
{
yield return new WaitForSecondsRealtime(0.5f);
SceneManager.LoadScene("Game");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613050036im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I make buttons in a list move up or down? 1 Answer
Menu works in stand alone but not in Game view in Editor 0 Answers
Buttons on menu prefab showing on scene view but not in game 0 Answers
Pause Menu problem 1 Answer
Scroll rect sound problem 0 Answers