- Home /
The question is answered, right answer was accepted
How to get sound to play and pause with the new UI
I'm trying to have a sound play on an empty game object using a UI button with the new UI system. I basically have this script on an empty game object with a sound file as sound source. But when I ad another button state and try and use the stop sound the button does not work at all, but I can get it to play with the first play sound option. I'm not good at C# so I'm sure I'm buggering this up with the new UI somehow. using UnityEngine; using System.Collections;
public class PodScannerSoundManager : MonoBehaviour {
public void ScannerPlay()
{
audio.Play();
}
public void ScannerStop()
{
audio.Stop();
}
}
In the button have you added the event trigger script and added your function there?
![buttons][1]
Yup I did that and both don't work, if I remove the bottom one then the 2nd one down works?? [1]: /storage/temp/39347-screen-shot-2015-01-22-at-54619-pm.png
I'm a little confused by the setup in the screenshot. It seems you have one button with three commands. The first command is telling an audiosource to play, and the second is telling something to both stop and play at the same time maybe?
If you want one button to control one sound then you only need one thing to trigger onClick. The other button should handle turning the sound off in it's onClick.
Answer by InvincibleCat · Jan 23, 2015 at 01:25 AM
It's normal because you are calling all your method on Click. So it will call audio.Stop() in the end...
Call just one (and only one) function from the button:
public void TogglePlay()
{
if(audio.isPlaying)
{
audio.Stop();
}
else
{
audio.Play();
}
}
Cheers
Ah, perfect, that did the trick. Thanks InvincibleCat I had been at that one for the better part of the day ;)
Follow this Question
Related Questions
Add Audio Source to a Prefab 2 Answers
Scroll rect sound problem 0 Answers
How to play multiple audioclips from the same object? 4 Answers
How Do I Get This To Play Sound? 0 Answers
How can I Get An Audio Clip to Play a Certain Number Of Times C# 2 Answers