Put this on “Unity Answers” but have now moved it to Forum because there’s no obvious way of doing it (yet).
I’ve got a menu set up with a series of colored buttons. The game is for kids who can’t read, so when they click a button a sound file plays with instructions.
I’ve got each button set up so that if the player repeatedly clicks on ONE of the buttons repeatedly, the playing sound stops and starts again from the beginning, so that the same sound doesn’t play over itself.
However, if the player repeatedly clicks MORE THAN ONE button, by going quickly and repeatedly from one button to another, the sounds for each button play over one another. because they’re kids, they are likely to do this!
Anyone please got any idea how I can prevent more than one sound being played at a time when they are activated from separate sources and from separate scripts?
The first answer I got was to write code like this for each button:
var audioSources[] : GameObject; // add all of your audio sources
if (GUILayout.Button("1")){
for(eachAudio in audioSources)
eachAudio.audio.Stop();
audio.clip = clip1;
audio.Play();
}
but when I enter the first line. The symbols threw up an error.
The second answer I got was to
.
I tried this by deleting all but one of the audio sources.
On a button without an audio source (redButton) I attached a code starting
var ohdear : AudioClip;
var press : AudioSource;
In the inspector of that object, I linked the “press” variable to the Audio Source of the single button that had one (blueButton). I then continued the code with
function OnMouseUp () {
press.audio.PlayOneShot (ohdear);
}
This still brings up an error of “There is no ‘AudioSource’ attached to the redButton game object, but a script is trying to access it”.
I’ve been tearing my hair out on this for three days now, and being in my 60’s I had hardly any hair to start with. My wife’s complaining and wearing sun glasses because of the glare.
Anyone please got any ideas of how I can do this?