- Home /
Why is my random sound java script not working?
Hi, I am trying to create a button that, when you press it, you get a beep and then the same beep when you release your touch AND THEN one second after the second beep, there will be a randomly chosen sound from an array of 50 sounds that could be played. I studied a similar problem from http://answers.unity3d.com/questions/161417/random-audio-clip-help.html and put all my sounds in the array. I get two errors. One is: Assets/MyScripts/MaleSoberSound.js(31,18): BCE0044: expecting (, found 'PlayRandom'.
and the other is: Assets/MyScripts/MaleSoberSound.js(31,31): UCE0001: ';' expected. Insert a semicolon at the end.
I don't know where to search to find what I'm doing wrong in my script. I would greatly appreciate help on this javascript I created. Thanks so much in advance!!! Tom #pragma strict
var sounds: AudioClip[];
function Start ()
{
}
function Update ()
{
}
function OnMouseDown ()
{
audio.Play();
//Application.OpenURL ("http://www.google.com");
Debug.Log ("I touched the Microphone!");
}
function OnMouseUp ()
{
audio.Play();
yield WaitForSeconds (1);
function PlayRandom () //call this function to play a random sound.
{
if (audio.isPlaying) return; // don't play a new sound while the last sound hasn't finished.
audio.clip = sounds[Random.Range(0,sounds.length)];
audio.Play();
}
Debug.Log ("I released the Mic button.");
}
Answer by LordZephyr · Dec 31, 2014 at 08:18 PM
Thank you so much #jimmycrazyskills!!! Without your help, I would not have been able to figure it out. The working script is below. Thanks again!!! Tom
#pragma strict
var sounds: AudioClip[];
var bip: AudioClip;
function Start ()
{
}
function Update ()
{
}
function OnMouseDown ()
{
audio.clip = bip;
audio.Play();
Debug.Log ("I touched the Microphone!");
}
function OnMouseUp ()
{
audio.clip = bip;
audio.Play();
yield WaitForSeconds (1);
if (audio.isPlaying) return; // don't play a new sound while the last sound hasn't finished.
audio.clip = sounds[Random.Range(0,sounds.length)];
audio.Play();
Debug.Log ("I released the Mic button.");
}
np mate , dont suppose you know anything about multiplayer servers?
Answer by jimmycrazyskills · Dec 31, 2014 at 06:23 PM
Hi mate you have your PlayRandom function inside your OnMouseUp function , I don't think you can do that I think it should help , sorry if it doesn't ;) below is what I think it should be like.
function OnMouseUp ()
{
audio.Play();
yield WaitForSeconds (1);
function PlayRandom (); //call this function to play a random sound.
Debug.Log ("I released the Mic button.");
}
function PlayRandom () //call this function to play a random sound.
{
if (audio.isPlaying) return; // don't play a new sound while the last sound hasn't finished.
audio.clip = sounds[Random.Range(0,sounds.length)];
audio.Play();
}
can't upvote yet, but looks like this should help a lot. +1
Hi, #jimmycrazyskills and thanks! I tried what you said and it kind of worked. I took the function PlayRandom out of the mouseup and nothing happened. BUT, I took away the "function PlayRandom ()" and it worked but only once. Each time I press the button after the first time, I continually get the same sound and no more beeps. Here is the updated script. Any suggestions on how I can get this script to reset itself each time? Thanks so much for your help... I'm getting closer to the answer! $$anonymous$$
#pragma strict
var sounds: AudioClip[];
function Start ()
{
}
function Update ()
{
}
function On$$anonymous$$ouseDown ()
{
audio.Play();
Debug.Log ("I touched the $$anonymous$$icrophone!");
}
function On$$anonymous$$ouseUp ()
{
audio.Play();
yield WaitForSeconds (1);
if (audio.isPlaying) return; // don't play a new sound while the last sound hasn't finished.
audio.clip = sounds[Random.Range(0,sounds.length)];
audio.Play();
Debug.Log ("I released the $$anonymous$$ic button.");
}
Your answer
Follow this Question
Related Questions
Sound playing at random. (JS) 2 Answers
play sound when moving stop sound when not 1 Answer
Random footsteps 5 Answers
Need Audio To Stop When I Touch a Game Object. 1 Answer
Random Audioclip. No Repeat ?? 1 Answer