|
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. 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?
(comments are locked)
|
|
Don't use PlayOneShot. Do: Of course, you need an AudioSource to be attached to the same GameObject as that script is. Let me know! 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)
|
|
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 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)
|

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.