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\$
1
  • \$\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, 2019 at 2:38

1 Answer 1

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\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .