The question is answered, right answer was accepted
Audio on keydown Won't stop
Hey, I made a script so that when I press "B"&"M" a sound is played. The problem is I only want it to play once. For example if you press that combination again the sound will not play. What do I need to add to my script?
using UnityEngine; using System.Collections;
public class PlayOnDestroy : MonoBehaviour {
public AudioSource Mysterysound;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.M)){
if(Input.GetKeyDown(KeyCode.B)){
Mysterysound.Play ();
}
} } }
Answer by Dragate · Nov 17, 2017 at 08:55 AM
If you want your Audiosource to play only once in your scene, you'll have to set a flag.
public AudioSource Mysterysound;
bool mysterysoundPlayed = false;
void Update () {
if(Input.GetKeyDown(KeyCode.M) && Input.GetKeyDown(KeyCode.B) && !mysterysoundPlayed){
Mysterysound.Play ();
mysterysoundPlayed = true;
}
}
As a side note, uncheck Loop checkbox in your Audiosource if you haven't already.
Thank you very much for the help. That is exactly what I was looking for! :)
Follow this Question
Related Questions
Recording Players voice in Unity using VR buttons. 0 Answers
Change button position using script 2 Answers
Trying to get the doors to play the opening and closing sounds seperately/individually 1 Answer
Move a separate object forward on collision between particle system and another object 0 Answers
Destroy gameobjects several times in a scene. Also destroy other instances of that gameobject. 2 Answers