Sound on Button OnClick and Loading and Scene
Hi There, I have a Button in which I have linked on the OnClick a function to load and Scene: SceneManager.LoadScene (1);
But I want also to have Sound when I click on the same button, so I have created an AudioSource, so again on the OnClick I have added another call for the AudioSource.Play
This works if don't execute my function that LoadScene, but when I have both of them on OnClick I'm not able to listen the sound.
Do you know if I'm missing something to get this working? Thanks in advance Yuri
Answer by Jessespike · Apr 04, 2016 at 09:02 PM
The sound is being cut off, because the script will load the next scene at the same-time. You can delay the scene load with a coroutine, replace the line where you have SceneManager.LoadScene(1)
with this:
StartCoroutine(DelaySceneLoad());
The coroutine can wait for a specified time, or if you have the AudioClip, you can instruct it to wait for the length of the sound instead.
IEnumerator DelaySceneLoad()
{
yield return new WaitForSeconds(1.0f);
SceneManager.LoadScene(1);
}
Thank you very much @Jessespike, I have applied your suggestion and works, also now I'm using the AudioClip length to have a more accurate time Regards Yuri
Your answer
Follow this Question
Related Questions
Update canvas in all scenes when edited? 0 Answers
Dont round decimal places? 2 Answers
Unity Web Player GUI mismatched with events & strange shadows 0 Answers
Unity performance questions 1 Answer