public AudioClip[] SoundEffect;
private AudioClip randomlySelectedAudio; // Add this line.
void Update () {
if (Input.GetButtonDown ("Fire1")){
randomlySelectedAudio = SoundEffect[Random.Range(0, SoundEffect.Length]; //Add this line
AudioSource.PlayClipAtPoint(randomlySelectedAudio, transform.position); //Change this line accordingly
StartCoroutine( PlayElsewhere() );
}
}
private IEnumerator PlayElsewhere(){
yield return new WaitForSeconds (5);
AudioSource.PlayClipAtPoint (randomlySelectedAudio, transform.Elsewhere);//Change this line accordingly
I've modified your code for basically when Clicking to "Fire1" button, it will select a random AudioClip and play it, also it will play the same audio on StartCoroutine function. So I've added a variable that will hold the randomly selected audio clip whenever player clicks "Fire1" button and assigned it to the places where you want to play audio(in "Update" and "PlayElsewhere")
↧