- Home /
Wait before playing audio
I didn't experienced it, but i have seen a lot of users complain about removing the PlayOneShot
. As far as i know you used it to play a sound only one time. I want to make a sound be played ONE TIME, wait a few seconds and then play again in a loop: play,wait,play,wait,play,wait, etc...
But how do i do that? I made this for now but it doesn't seems to work:
#pragma strict
var Enemy : Transform;
var distance = 150;
var Signal : AudioSource;
private var Close = false;
var timer : float; //The time it was supposed to wait.
function Update()
{
if(Vector3.Distance(transform.position, Enemy.position) < distance)
{
Close = true;
Signal.Play(timer); //Doesn't wait.
//Can't add yield wait for seconds here because it's in an Update function.
}
else
{
Close = false;
}
}
Answer by Kapel · Nov 05, 2016 at 05:03 PM
Use AudioSource.isPlaying to detect if playing is done then start Coroutine and again AudioSource.Play()
I hope this code i made special for you will explain you how to do this.
using UnityEngine;
using System.Collections;
public class AudioS : MonoBehaviour {
bool PlaySound;
// Use this for initialization
void Start () {
PlaySound = true;
}
// Update is called once per frame
void Update () {
if (PlaySound == true)
{
GetComponent<AudioSource>().Play();
PlaySound = false;
}
if(GetComponent<AudioSource>().isPlaying == false)
{
StartCoroutine(Wait());
}
}
IEnumerator Wait()
{
yield return new WaitForSeconds(2.0f);
PlaySound = true;
}
}
You can also use Invoke instead of coroutine.
There are some errors in the script refering to the PlaySound
using UnityEngine;
using System.Collections;
public class AudioS : $$anonymous$$onoBehaviour {
bool PlaySound;
// Use this for initialization
void Start () {
PlaySound = true;
}
// Update is called once per frame
void Update () {
if (PlaySound == true)
{
GetComponent<AudioSource>().Play();
PlaySound = false;
}
if(GetComponent<AudioSource>().isPlaying == false)
{
StartCoroutine(Wait());
}
}
IEnumerator Wait()
{
yield return new WaitForSeconds(2.0f);
PlaySound = true;
}
Impossible, Because this code works for me without any errors. In your code i see you are using JS and my code is C#. Be sure you paste this code to C# file or adapt it for JS.
Yeah, actually now it worked! I think it is because my engine is bugging and my Scripts are being ignored and all sorts of things don't ask me why.
But now i've realised something... Where do this fit in my script? Like... How am i supposed to use this in my script? I need my script's basic functions and this, not just play a sound =\
It should be something like that or simillar
if(Vector3.Distance(transform.position, Enemy.position) < distance && PlaySound == true)
{
GetComponent<AudioSource>().Play();
PlaySound = false;
}
if(GetComponent<AudioSource>().isPlaying == false)
{
StartCoroutine(Wait());
}
// Rest of your code
...
}
IEnumerator Wait()
{
yield return new WaitForSeconds(2.0f);
PlaySound = true;
}
Answer by vulgerstal · Nov 05, 2016 at 10:26 PM
using UnityEngine;
using System.Collections;
public class vulgerstalAudioLoopDelayed : MonoBehaviour {
public float delay;
float delayTemp;
// Use this for initialization
void Start () {
delayTemp = delay;
}
// Update is called once per frame
void Update () {
if (GetComponent<AudioSource>().isPlaying)
delay = delayTemp;
else
delay -= Time.deltaTime;
if (delay <= 0)
GetComponent<AudioSource>().Play();
}
}
Your answer
Follow this Question
Related Questions
Loop animation with a wait in between 3 Answers
Instantiate color change loop 0 Answers
Yield WaitForSeconds Not Working. Coroutine. 1 Answer
Audio clip not playing 1 Answer
Playing Sound 2 Answers