Home > OS >  Why the sound effect does not work in unity
Why the sound effect does not work in unity

Time:05-10

Why the sound effect does not work in unity? This is my script to move to the next scene using buttons:

 public void NewGame()
    {
        SceneManager.LoadScene("level 1");
    }
when I try to add a sound effect, it doesn't work

public AudioClip impact;
    public void NewGame()
    {
        AudioSource.PlayClipAtPoint(impact, transform.position);
        SceneManager.LoadScene("level 1");
    }

CodePudding user response:

You're making the audio play, and then immediately loading in a different scene. The object that is playing the audioclip also unloads.

You could offset the two by making NewGame() a coroutine instead, with a small timed offset between the PlayClipAtPoint() call and the LoadScene() call.

CodePudding user response:

As Alex Leest said, you are loading new scene right after audio play.

When unity scene loading started, all gameobject except DontDestroyOnLoad Object are destroyed.

So, you may add code below to your Audio Manage class. (Also, you should ensure that your Audio Manager class always has only one instance in your whole program for not cause bugs)

private void Awake()
{
    DontDestroyOnLoad(this);
}
  • Related