- Home /
Sound audio.PlayOneShot
Hi, i have a problem with the sound effect on my buttons, when i press the button the sound play perfectly, but when i added Application.LoadLevel (); to my script, the sound cuts, i think it loads the other scene before the sound plays totally.
Is there a solution for that ?
Thanks a lot
public var sound1 : AudioClip;
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider.name == "button")
{
audio.PlayOneShot(sound1);
}
}
if (Input.GetMouseButtonUp(0))
{
Application.LoadLevel ("Level 1");
}
}
Answer by serenefox · May 19, 2014 at 07:38 PM
It is because you are loading the level at the same time you are playing the sound. Try a coroutine with the "yield Waitforseconds". Since I haven't used Javascript in a while (which it looks like you are using), it might be a little off:
#pragma strict
public var sound1 : AudioClip;
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider.name == "button")
{
audio.PlayOneShot(sound1);
}
}
if (Input.GetMouseButtonUp(0))
{
LoadLevelWait();
}
}
function LoadLevelWait()
{
yield WaitForSeconds(1);
Application.LoadLevel ("Level 1");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613145422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
During sound between scenes 1 Answer
(help) Door sound 1 Answer
How to resolve Samsung App Store submission failure 1 Answer
Weird Sounds behaviour 0 Answers
Cant find my path to sound folder 1 Answer