0
\$\begingroup\$

I am trying to play the sound of the button click before loading the scene in unity. However, the sound is never played. Below is my code, help would be appreciated.

public void RestartGame()
{
    audioManager.PlaySound(forwardSound);
    StartCoroutine(DelaySceneLoad("Gameplay"));
}

IEnumerator DelaySceneLoad(string scene)
{
    yield return new WaitForSeconds(forwardSound.length);
    GameTracker.SetGameOver(false);
    SceneManager.LoadScene(scene);
}
\$\endgroup\$
  • \$\begingroup\$ Your code looks fine. Do you get any errors from the PlaySound line? Have you ensured your sound is pre-loaded so it's not delayed by streaming from disc? Can your show us the relevant audioManager code so we can check for errors there? \$\endgroup\$ – DMGregory Mar 25 at 2:38
2
\$\begingroup\$

SceneManager.LoadSceneAsync() might be of interest to you, it lets you do lots of cool things such as loading screens and more before a scene completes loading and changes.

Instead of waiting for the length of your audio clip (which might not be loaded and played completely from memory in time), try checking to see if the AudioSource its attached to isPlaying and load the scene when it sets to false.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.