Unity Sound Does Not Play

I try to play a sound but it does not play

Here is my code:

public void Replay()
{
  playAudio ();
  Application.LoadLevel (Application.loadedLevel);
}

void playAudio()
{
    AudioSource audio = GetComponent<AudioSource> ();

    audio.Play();
}

When the button is pressed, I call Replay(). But the sound does not play.

If I noticed Application.LoadLevel (Application.loadedLevel);, the sound plays normally.

What should I do to make sound play with Application.LoadLevel()?

+4
source share
3 answers

The sound source reproducing the sound is deleted before it can finish.

Here is an alternative solution that uses the output to wait for the sound to finish.

public void Replay()
{
    StartCoroutine("ReplayRoutine");
}

IEnumerator ReplayRoutine()
{
    AudioSource audio = GetComponent<AudioSource>();

    audio.Play();
    yield return new WaitForSeconds(audio.clip.length);

    Application.LoadLevel(Application.loadedLevel);
}
+1
source

You call the playback method, and you load the scene after it. Try calling playAudio()before the load level.

public void Replay() {
    Application.LoadLevel(Application.loadedLevel);
    playAudio();
}
0
source

, , , , . , . ( , Start())

0

Source: https://habr.com/ru/post/1620290/


All Articles