- Home /
WaitForSeconds Audio Help
I am working on a game and when the character dies a sound plays, but the gameover screen loads before the sound can finish playing. My first solution to the problem was to use the WaitForSeconds function to wait the length of the sound file before loading the gameover screen, but it isn't changing within the game.
function OnCollisionEnter (collision : Collision)
{
//reset the light settings when we pick up a battery
switch(collision.gameObject.tag)
{
case "Battery":
//Need Modifaction to dim, not range
gameObject.transform.FindChild("Flashlight").light.range += 10.0;
Destroy(collision.gameObject);
break;
case "UVpowerup":
powerupState = POWERUP_UV;
Destroy(collision.gameObject);
//change light filter
break;
case "Bottom":
audio.PlayOneShot(splashSound, 0.4);
splashAnimation.particleSystem.Play();
yield WaitForSeconds(.65);
GameOver();
break;
}
The audio plays for a bit then gets cut off. I tried putting in 10 seconds to see what would happen but nothing worked. Any Ideas?
Check your console for any errors or warnings.
Check the sound, make sure that it's importing correctly and that it's about what you expect.
$$anonymous$$aybe try using Debug.Log()
or the $$anonymous$$onoDevelop debugger to check your code as it's running -- what is splashSound.length, when does it start playing, when does your yield finish, and so on.
Other option: what function is all of this in? A little more context might help catch the problem.
Answer by JayMHelpsU · Apr 13, 2012 at 09:23 PM
It might be because the WaitForSeconds is in milliseconds, i know it sounds odd but i have seen this before when coding. Try putting in 1000 seconds and see what happens. Happy coding :) good luck
Answer by gregzo · Apr 13, 2012 at 10:34 PM
Don't use PlayOneShot. Do:
audio.clip = splashsound;
audio.Play();
while(audio.isPlaying)
{
yield;
}
GameOver();
Of course, you need an AudioSource to be attached to the same GameObject as that script is.
Let me know!
$$anonymous$$akes sense, but still not working :/. The script is attached to the playercapsule, as is the AudioSource. The clip plays for about a third of a second then cuts out and the gameover screen loads.
Have you tried playing the sound in Unity itself, in the preview window? Perhaps the sound file itself is too short?
Then your clip is a third of a second long... $$anonymous$$idding. Something must be wrong elsewhere. $$anonymous$$y script works, tried and approved.
I agree with @gregzo: the problem must be caused by something else. This OnCollisionEnter is a coroutine, thus GameOver() is executed only after the delay. Is there something else that could destroy the object that contains the audio source? Some old forgotten script, for instance? If something is loading a new (or the same) level, all objects are destroyed, including this audio source.
Yeah, the clip plays all the way through in the preview window. Honestly, I'm just going to assume its my crappy computer, because the game is lagging as well (and its a relatively simple game, no reason for it to be).
Your answer
![](https://koobas.hobune.stream/wayback/20220613070307im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Play sound while moving. 2 Answers
someone is there? 1 Answer