x


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?

more ▼

asked Apr 13 '12 at 06:56 PM

depento gravatar image

depento
1 1 1 3

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.

Maybe try using Debug.Log() or the MonoDevelop 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.

Apr 13 '12 at 07:34 PM rutter
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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!

more ▼

answered Apr 13 '12 at 10:34 PM

gregzo gravatar image

gregzo
1.6k 31 42 51

Makes 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.

Apr 13 '12 at 11:06 PM depento

Have you tried playing the sound in Unity itself, in the preview window? Perhaps the sound file itself is too short?

Apr 13 '12 at 11:24 PM POLYGAMe

Then your clip is a third of a second long... Kidding. Something must be wrong elsewhere. My script works, tried and approved.

Apr 13 '12 at 11:25 PM gregzo

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.

Apr 13 '12 at 11:57 PM aldonaletto

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).

Apr 16 '12 at 04:16 AM depento
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Apr 13 '12 at 09:23 PM

JayMHelpsU gravatar image

JayMHelpsU
120 4 6 7

Tried it, didn't work :/. Thanks though!

Apr 13 '12 at 10:02 PM depento

Nope, WaitForSeconds is in seconds.

Apr 13 '12 at 10:30 PM gregzo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5274
x2059
x1060
x175

asked: Apr 13 '12 at 06:56 PM

Seen: 651 times

Last Updated: Apr 16 '12 at 04:16 AM