I’m using a random array to select an audio clip, and need to send whichever clip is selected to multiple parts of my code simultaneously, including Coroutines. I’m using the Coroutines so i can use the yield return new WaitForSeconds command.
My problem is that, the way it’s written now, the random sound will be selected separately in each piece of code, so both sources won’t necessarily be playing the same thing. Is there a way i can trigger an audioclip to be randomly selected on GetButtonDown, which can then be somehow referenced by both the Coroutine and the GetButtonDown? I apologise in advance if this has an easy solution that i’ve overlooked, or has been answered elsewhere; i’m very new to C#/any code at all, and have been squinting and scratching my head over this for a few hours now.
The code looks something like this (i’ve stripped out anything that seemed irrelevant):
public AudioClip[] SoundEffect;
void Update () {
if (Input.GetButtonDown ("Fire1")){
AudioSource.PlayClipAtPoint(SoundEffect [Random.Range (0, SoundEffect.Length)], transform.position);
StartCoroutine( PlayElsewhere() );
}
}
private IEnumerator PlayElsewhere(){
yield return new WaitForSeconds (5);
AudioSource.PlayClipAtPoint (SoundEffect [Random.Range (0, SoundEffect.Length)], transform.Elsewhere);//i know transform.elsewhere probably doesn't exist, it's just a stand-in