- Home /
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.
Answer by clunk47 · Sep 07, 2013 at 05:00 PM
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;
}
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.
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 $$anonymous$$ch 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.
Thanks very much, for the help and the advice. :)
$$anonymous$$any tutorials, including basics of how to use the interface http://unity3d.com/learn/tutorials/modules
There is a bug in the script, it will never play the last AudioClip in the array. The second parameter (max) to Random.Range is exclusive (i.e. it should be just clips.Length).
Answer by mirkobon · Sep 07, 2013 at 01:58 PM
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();
}
}
}
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?
Your answer
Follow this Question
Related Questions
Walking sound Script returns with Error: BCE0077 2 Answers
Play a sound when the wind blows? 0 Answers
I want my trigger sound only to play once! 0 Answers
Sound help with script 1 Answer
Sound on collisions 1 Answer