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 Fady-S · Aug 21, 2012 at 08:29 AM · audiosourceaudioclipyield waitforseconds

Using yield WaitForSeconds after AudioSource.PlayClipAtPoint

I am trying to play a sound (ticking) right before an explosion. the ticking sound will not play correctly if I use yield WaitForSeconds(), I will have a buzzing sound instead of a ticking one.

if(condition){

AudioSource.PlayClipAtPoint(exploTick,gameObject.transform.position);

yield WaitForSeconds(exploTick.length);

placeExplosion=Instantiate(electExplode,gameObject.transform.position,Quaternion.identity);

placeExplosion.enableEmission=true;

Removing "yield WaitForSeconds" will make the audio clip to play correctly.

Any thoughts?

Comment
Add comment · Show 3
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 Fattie · Aug 21, 2012 at 12:48 PM 0
Share

Issues like 'yield" and "coroutines" are only for very advanced programmers. there is utterly no need to use them to do something so incredibly simple.

Just use "Invoke" which is the basic command used to program video games.

avatar image Fady-S · Aug 21, 2012 at 03:11 PM 0
Share

Thanks alucardj for giving me a hint. Finally I got it to work by testing and forcing the audio clip to play just once.

private var onePlay:boolean=false;

if(condition){

if(onePlay) //This will not evaluate for the first frame.The audio clip will play

return;

AudioSource.PlayClipAtPoint(exploTick,gameObject.transform.position);

onePlay=true; //Switch the value to true and the clip will never be played again

yield WaitForSeconds(exploTick.length);

placeExplosion=Instantiate(electExplode,gameObject.transform.position,Quaternion.identity);

placeExplosion.enableEmission=true;

Now the audio clip is playing as it should.

avatar image AlucardJay · Aug 21, 2012 at 03:12 PM 0
Share

No worries (wish I didn't delete my comment now!)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Aug 21, 2012 at 01:42 PM

You can use a coroutine, but be sure to not call it more than once - or you will get that buzzing sound and a memory crowded by tons of coroutine instances:

var timerOn = false; var tickInterval: float = 0.5;

function TimeBomb(timer: float){ if (timerOn) return; // abort new calls to TimeBomb timerOn = true; // signal that TimeBomb is running while (timer > 0){ // while timer is counting... audio.Play(); // play the tick sound... yield WaitForSeconds(tickInterval); // and wait a decent interval timer -= tickInterval; // count the time elapsed } // timer zeroed - explode the bomb: placeExplosion = Instantiate(electExplode, transform.position, Quaternion.identity); placeExplosion.enableEmission=true; timerOn = false; // TimeBomb has finished } Add an AudioSource to the bomb and set its Clip field to the desired tick sound. Call TimeBomb(timeToExplode) when you want to start the timer. In order to avoid starting multiple TimeBomb coroutines, only call TimeBomb when timerOn is false (the coroutine already does that, but at that point a memory block was already allocated - if called each frame, a lot of unused memory blocks will be generated, causing more frequent garbage collections - a time consuming internal operation)

Comment
Add comment · Show 1 · 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 Fady-S · Aug 21, 2012 at 02:12 PM 0
Share

Thanks Aldo, well I did exactly what you have suggested but without adding an audio source:

private var onePlay:boolean=false;

if(condition){

if(onePlay) return;

AudioSource.PlayClipAtPoint(exploTick,gameObject.transform.position);

onePlay=true;

yield WaitForSeconds(5.0);

placeExplosion=Instantiate(electExplode,gameObject.transform.position,Quaternion.identity);

placeExplosion.enableEmission=true;}

Now everything plays nicely.

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

10 People are following this question.

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

Related Questions

A better script for a machine gun? 1 Answer

Fade, Then Stop all other audiosources attached to gameobject? 1 Answer

Help with IEnumerator and if sound is playing 2 Answers

OnTriggerExit stop audio 1 Answer

Instanced Prefabs: Audio clip array out of range 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