I’m probably doing something wrong (obviously), can someone please tell me what it is?
it’s a button and I want it to stop the audio (a song) the second time I press the button. Basically to toggle the song on and off.
function OnMouseDown ()
{
if (buttonRange == true)
{
if (audio.isPlaying)
{
audio.clip = clickSound;
audio.Stop();
}
else
{
audio.PlayOneShot(clickSound);
}
}
Debug.Log ("Pressed Button");
}
audio.isPlaying checks if the default clip of the AudioSource is playing. It doesn’t check if any other clips being played by the AudioSource are playing.
clickSound must not be the default clip which is why audio.isPlaying always returns false.
You can make clickSound the default clip by doing:
audio.clip = clickSound;
I see that you did that in your code, but you did it inside the if statement checking if audio.isPlaying is true or false. But since it’s false from the beginning, clickSound is never the default clip.
I think this would work:
function OnMouseDown ()
{
if (buttonRange == true)
if (audio.isPlaying)
{
audio.clip = clickSound; //This looks redundant
audio.Stop();
}
else
{
audio.PlayOneShot(clickSound);
audio.clip = clickSound; //I added this
}
}