I’m looking for a way to make a script that waits for a few seconds then plays a random noise (I would select the noises it would be able to choose in the script) then waits again and chooses a random noise again and again until the scene has been finished. Is this possible and if so what functions should I be using? Sorry if this is asking too much. Thanks in advance.
Here’s a more simplified example. You assign each clip you want in your array using the inspector. Then of course you need to place your own conditions to make the boolean true. This gives clip index a random range between 0 (first index), and clips.Length - 1 (last index) of the array. Also be sure to check that the audio is not playing before you try to play it.
#pragma strict
var clips : AudioClip[];
var clipIndex : int;
var playAudio : boolean = false;
@script RequireComponent(AudioSource);
function Update()
{
PlaySound();
}
function PlaySound()
{
yield WaitForSeconds(Random.Range(1, 10));
if(playAudio && !audio.isPlaying)
{
clipIndex = Random.Range(0, clips.Length - 1);
audio.PlayOneShot(clips[clipIndex], 1.0);
}
playAudio = !playAudio;
}
yes it is here is the script i tested:
#pragma strict
// add as many AudioSources as you want
var Audio1 : AudioSource;
var Audio2 : AudioSource;
var Audio3 : AudioSource;
var Audio4 : AudioSource;
var PlayAudio : int;
function Start () {
}
function Update () {
// Dont forget to change the max number if you incres the amount of audio sources
PlayAudio = Random.Range(1, 4);
Playsound();
}
function Playsound()
{
yield WaitForSeconds (5);
{
// if you added audiosources make sure to add them in the play sound
if (PlayAudio == 1)
{
Audio1.Play();
}
if (PlayAudio == 2)
{
Audio2.Play();
}
if (PlayAudio == 3)
{
Audio3.Play();
}
if (PlayAudio == 4)
{
Audio4.Play();
}
}
}