Question by
_C1 · Mar 03, 2016 at 11:11 AM ·
c#clickmouseclick
Turn on and off sound on mouse click C#
Hey,
I have an object and when you click on it with the mouse the sound will play. I want to be able to turn the sound off when I click on it again. This is what I have so far:
public AudioClip mySound;
void Start () {
}
void OnMouseDown ()
{
if (Input.GetMouseButtonDown (0))
GetComponent<AudioSource> ().PlayOneShot (mySound);
}
}
Thanks.
Comment
Best Answer
Answer by Veerababu.g · Mar 03, 2016 at 12:24 PM
all what you need is just like pause button. if you press first time music has to play, if you hit again music has to stop. if so then try this
bool play;
if (Input.GetMouseButtonDown (0)){
play = !play;
}
if(play){
// play your music
}else {
// stop your music
}
Your answer
Follow this Question
Related Questions
Move only if mouse click the Terrain 0 Answers
How to move a game object to a position after selecting it 0 Answers
Match 3 Movement Using Keyboard Controls,Match 3 movement using keyboard controls 1 Answer
Move camera between canvases in UI menu 0 Answers
HOW TO MOVE A OBJECT TO ANOTHER OBJECT SMOOTHLY NOT INSTANTLY? 0 Answers