Is it possible to make a random sound script?

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.

more ▼

asked Sep 07, 2013 at 01:05 PM

SilverHorrorDev gravatar image

52 889 27

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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()


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;
more ▼

answered Sep 07, 2013 at 05:00 PM

clunk47 gravatar image

36.4k 2 11 94

Thanks very much for this. Could you explain a bit more on how to use this script? I'm really new to coding, so sorry if I'm being a bit stupid about this.

Sep 07, 2013 at 05:33 PM SilverHorrorDev

copy the script onto an object, then you will be able to see it's variables in the Inspector, you will see it has an audio array, it's an array of sounds, fill the array with sounds, for example 5 sounds, and then play unity. If you want to learn how to make a game at all, you will have to get into the habit of playing with scripts for one hour to discover what they do and to learn about them by yourself, one hour of study will teach you more than any amount of questions you could ask people here.

I just spent 3 days learning shader language, all you do is take a script, change some of the numbers, copy lines from similar scripts, research what the words mean on unity reference, and soon you will actually be a game developer.

Sep 07, 2013 at 05:52 PM MountDoomTeam

Thanks very much, for the help and the advice. :)

Sep 07, 2013 at 05:56 PM SilverHorrorDev

Many tutorials, including basics of how to use the interface http://unity3d.com/learn/tutorials/modules

Sep 07, 2013 at 06:57 PM clunk47
(comments are locked)
10|3000 characters needed characters left

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);

function Playsound()
yield WaitForSeconds (5);
// if you added audiosources make sure to add them in the play sound
if (PlayAudio == 1)
if (PlayAudio == 2)
if (PlayAudio == 3)
if (PlayAudio == 4)
more ▼

answered Sep 07, 2013 at 01:58 PM

mirkobon gravatar image

91 1 9 16

if this helped you please mark it as accepted

Sep 07, 2013 at 02:11 PM mirkobon

Thank you very much for the script, but there is one problem. I get this error when trying to use it: Assets/Standard Assets/Script/Random Ambience.js(25,1): BCE0043: Unexpected token: if.

Could you help me with this?

Sep 07, 2013 at 02:36 PM SilverHorrorDev
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Sep 07, 2013 at 01:05 PM

Seen: 410 times

Last Updated: Sep 07, 2013 at 06:57 PM