The question is answered, right answer was accepted
How to stop audio on button press?
Hi, I'm sorry to post with such a simple question but I've been going round in circles for hours now and I'm seriously frustrated. Firstly, I'll happily admit I have NO IDEA what I'm doing. I know exactly what I want to achieve, and I've got the basics of it down (much to my surprise) but I am running into one problem that I can't for the life of me figure out.
I want to have a series of buttons with audio clips assigned to them. I want to press the button and the audio that corresponds to the button I've pressed play. I then want to press a different button, have whatever audio is already playing stop, and then have the audio of that button play. I've got as far as creating the buttons and getting the audio to play using the On Click () function in the Inspector tab, but as for getting the audio that is already playing to stop, I have no idea. I've tried putting more functions in the On Click () tab (eg placing AudioSource.Stop above AudioSource.Play), I've googled many other threads on the same subject and tried various scripts, but I don't even know if I'm using scripts right. I've been going in circles for the best part of 8 hours now and I'm just about ready to lose my sanity.
If anyone could possibly help, I would seriously appreciate it. I need it explaining in the way you would explain it to a helper monkey, because how I've got this far already is a miracle. I had some experience making a very similar app using Action Script a few years ago, but any memory of that has long left me.
Put every button to test when they are pressed and when mouse button is pressed. When mouse button is pressed but not hit to the button, stop the sound.
Then you could make some static variable that tells what button is pressed and button stops the sound if it differs from its id.
Thanks for the reply. It seems to be possible without needing a "Stop" button as described in the following thread, I just don't know how to implement or attach the script to the button press - http://answers.unity3d.com/questions/194110/how-to-stop-all-audio.html
You could use that script too. Like:
OnClick() {
StopAllAudio();
GetComponent<AudioSource>().Play();
}
That's the problem, I have no idea how. I've tried looking at tutorials but it's always game based stuff that I really don't need to know for what I'm doing. I've tried scripting all of the actions I need (stop all audio playing, play certain audio file, loop that file) on a button press but I don't know enough to be able to do it correctly. With Action Script, it was a case of doing exactly that, scripting so on a button press it would stop all audio, play the defined audio file and then loop until either the next button was pressed or until stop was pressed, but I can't seem to manage to work out how to write that in C#.
Answer by A3tra3rpi · Dec 29, 2015 at 02:40 PM
private AudioSource AS;
private AudioSource[] AllAudioSources;
void Start () {
AS = GetComponent<AudioSource> (); //Audio source of this button
AS.Stop();
AS.enabled = false;
AS.loop = true;
AllAudioSources = GameObject.FindObjectsOfType (typeof(AudioSource)) as AudioSource[]; //all audio sources
}
void StopAllAudio() {
foreach (AudioSource Audio in AllAudioSources) {
Audio.Stop();
Audio.enabled = false;
}
}
void Clicked() {
StopAllAudio ();
AS.enabled = true;
AS.Play ();
}
void OnMouseDown() {
Clicked ();
}
//Which one you want to use. Im not sure about OnClicked but OnMouseDown worked for me on computer
void OnClicked() {
Clicked ();
}
}
Thanks for that! I can't seem to get it to work though, I'm convinced it's me that's doing something wrong rather than the script being wrong. Can you point me in the right direction of how I would attach the script to a button? I've followed so many different suggestions today that I think I'm confusing one with another and I'm doing something wrong.
I haven´t used OnClick(), but On$$anonymous$$ouseDown() needs collider to detect. What kind of button are you using. GUI Texture or mesh?
For all I know I've gone about the whole thing from start to finish wrong, I've just been Googling what I need to know when I've needed to know it. It got me this far, but now I'm stuck!
So. If your buttons are objects, with that button texture (and collider), you can just use this. If your buttons are GUI Textures you can use http://docs.unity3d.com/ScriptReference/GUIElement.HitTest.html ins$$anonymous$$d of OnClicked(). If your buttons are GUI Buttons created via script, none above.