var sound:AudioClip[];
var DelayToWait : float;
function Awake () {
}
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward,hit, 10)) {
if(hit.collider.gameObject.tag=="shock"){
audio.clip = sound[Random.Range(0,sound.length)];
audio.Play(); ;
yield WaitForSeconds(DelayToWait);
}
}
}
I’d like to play those sounds and wait 5 or 6 seconds between each raycast hit , but i can’t put a coroutine in the Update function anyone know how to do that ?
As you’ve mentioned you can’t yield inside Update. What you can do is to either invoke or loop a coroutine outside of Update or have a timer inside Update. I notice you have an unnecessary overhead with hit.collider.gameObject.tag as a collider already has the tag as a inherited variable. You also have a weird semicolon on line 14.