- Home /
Every few seconds, play an audio clip
Working on a game, where every few seconds, I want an object to play a sound. I'm using WaitForSeconds, but I'm getting some errors.
using UnityEngine;
using System.Collections;
public class Carsound : MonoBehaviour {
public AudioClip awoogah;
public int carwait = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
yield return new WaitForSeconds (carwait);
{
audio.PlayOneShot(awoogah);
Debug.Log("ChOO-ChOO");
}
}
}
Any help would be greatly appreicated!
Try this, but you have to change startPlay to true,
public AudioClip awoogah;
private bool startPlay = false;
// Update is called once per frame
void Update ()
{
if(startPlay){
StartCoroutine(PlaySomeSound(10.0F));
startPlay = false;
}
}
IEnumerator PlaySomeSound(float carWait)
{
yield return new WaitForSeconds(carWait);
audio.PlayOneShot(awoogah);
}
This would actually result in a weird situation as you would start a new coroutine on each frame. You need to set startPlay back to false at some point.
Answer by MarkFinn · Dec 04, 2012 at 05:27 AM
You'd be better off moving the entire thing to a coroutine I reckon. It'll give you a lot more flexibility. (Also, you cannot yield from any of the standard Unity Methods.
using UnityEngine;
using System.Collections;
public class Carsound : MonoBehaviour {
public AudioClip awoogah;
public int carwait = 10;
bool keepPlaying=true;
void Start () {
StartCoroutine(SoundOut());
}
IEnumerator SoundOut()
{
while (keepPlaying){
audio.PlayOneShot(awoogah);
Debug.Log("ChOO-ChOO");
yield return new WaitForSeconds(carwait);
}
}
}
You can yield in nearly all of the standard Unity methods, aside from Update-related functions (FixedUpdate, LateUpdate etc.) and Awake.
Got it to work. Thanks $$anonymous$$arkFinn and all who posted.
Answer by fafase · Dec 04, 2012 at 07:48 AM
Fact is you cannot yield in Update. So:
void Update () {
yield return new WaitForSeconds (carwait);<- Here error
{<-Why is this by the way?
audio.PlayOneShot(awoogah);
Debug.Log("ChOO-ChOO");
}
}
The most simple way is InvokeRepeating:
void Start(){
InvokeRepeating("PlaySound",0.001f,2f);
}
void PlaySound(){
audio.Play();
}
The first parameter is the function to call. The second is when you want the first call to happen. 0 is buggy and call the function twice so instead give a small value like I did. The last parameter is the frequency of calls. In my case every 2 seconds. Make sure your sound is not looping.
Now other way. even easier!!! Your sound could be the length you need (you could use Audacity). So for instance it goes Choo-chooo.....silence...... Then all you have to do is attach the AudioSource to your train (I guess it is a train), tick PlayOnAwake and tick Loop. And that is it.
Answer by Neo.Yang · Dec 04, 2012 at 07:09 AM
Hi,Msurdej!
Few days ago, I faced a similar problem like yours. actually. the [ExecutionOrder][1] --> "Normal coroutine updates are run after the Update function returns. A coroutine is function that can suspend its execution (yield) until the given given YieldInstruction finishes."
Make it to easy understand, update method running every frame,the coroutine will follow, but the frame rate is not stable. So the time will not always accurate.
The solution is that you need a independent timer, for me I choosed the fixedUpdate Method, if you didn't change the Time.deltaFixedTime(Defalut is 0.02s,You can check the [Time Manager][2]). So you can write your script like this :
float fixedTimer = Time.fixedDeltaTime;
float accumulateTime = 0;
int carwait =10;
void fixedUpdate()
{
accumulateTime += fixedTimer;
// Some times the system round algorithm will failed. use this to make sure the time always be accurate.
accumulateTime = (float)System.Math.Round(accumulateTime,3);
if(accumulateTime >= carwait)
{
//Play the audio...
accumulateTime -= carwait;
}
}
For now, my script is work fine. if you got new ideas and work precisely , plz tell me. [1]: http://docs.unity3d.com/Documentation/Manual/ExecutionOrder.html [2]: http://docs.unity3d.com/Documentation/Components/class-TimeManager.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
play audio for few seconds on key hit 1 Answer
Playing audio 1 Answer
Fade in AudioLister over time #c 1 Answer