Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by depento · Apr 13, 2012 at 06:56 PM · audiowaitforseconds

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Apr 13, 2012 at 07:34 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image depento · Apr 13, 2012 at 10:02 PM 0
Share

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

avatar image gregzo · Apr 13, 2012 at 10:30 PM 0
Share

Nope, WaitForSeconds is in seconds.

avatar image
0

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!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image depento · Apr 13, 2012 at 11:06 PM 0
Share

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

avatar image POLYGAMe · Apr 13, 2012 at 11:24 PM 0
Share

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

avatar image gregzo · Apr 13, 2012 at 11:25 PM 1
Share

Then your clip is a third of a second long... $$anonymous$$idding. Something must be wrong elsewhere. $$anonymous$$y script works, tried and approved.

avatar image aldonaletto · Apr 13, 2012 at 11:57 PM 0
Share

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.

avatar image depento · Apr 16, 2012 at 04:16 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Play sound while moving. 2 Answers

someone is there? 1 Answer

Change Music When GameObject is Grabbed? 1 Answer

Stop another sound from another script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges