- Home /
Hi guys I am wondering how can I stop the audio from playing if I click the object twice?
This is the Script that I am using
using UnityEngine;
using System.Collections;
public class OnMouse_playAudio : MonoBehaviour {
// Use this for initialization
// Use this for initialization
void Start () {
}
void OnMouseDown()
{
audio.Play();
}
}
Answer by Jeff-Kesselman · Jun 03, 2014 at 05:47 PM
to stop your audio, use audio.Stop(); see http://docs.unity3d.com/ScriptReference/AudioSource.Stop.html
To start on one click and stop on the next, use a boolean
if you don't know what a boolean is or how to use one, I strongly suggest you start with this book:
http://www.amazon.com/Unity-Game-Development-Essentials-Goldstone/dp/184719818X
Answer by kingdavid127 · Jun 03, 2014 at 06:18 PM
This might work.
using UnityEngine;
using System.Collections;
public class OnMouse_playAudio : MonoBehaviour
{
bool audioBool = false;
void OnMouseDown()
{
audioBool = !audioBool;
if(audioBool)
audio.Play();
else
audio.Stop();
}
}
Your answer
Follow this Question
Related Questions
onFilterAudioRead Problems 0 Answers
Slowly fade audio's pitch on key press? 3 Answers
Stereo Mix as Input? 1 Answer
[c#]Does audio.Play() work only in Update and Start funcitons? 2 Answers
Where are the alarm audio files for the Stealth tutorial? 1 Answer