How do I get my audio to play once?
My current script is
using UnityEngine;
using System.Collections;
public class AudioTrigger : MonoBehaviour {
public AudioSource source;
public AudioClip clip;
// Use this for initialization
public void Awake () {
source = GetComponent<AudioSource> ();
}
// Update is called once per frame
public void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag ("Player")) {
source.Play ();
}
}
}
How Would I go about playing it once when triggered then disabled forever.
Answer by allenallenallen · Sep 21, 2015 at 04:07 PM
Simple. Use a boolean.
private bool canRunAudio = true;
public void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag ("Player") && canRunAudio) {
canRunAudio = false;
source.Play ();
}
}
Im sorry I'm quite new to all this. Would I attach this as a new script or edit it into the alreadt made one.
please help me, I tried to put a 2D button, but
public void OnTriggerEnter (Collider other) { if (other.gameObject.Comparetag ("player") && canRunAudio) {
error output how to convert it to 2D button
Your answer
Follow this Question
Related Questions
I want to press a Button and play a sound? 2 Answers
A more realistic sound of the vehicle. 1 Answer
Game Show Happy Birthday whenever Birthday Will Come 1 Answer
How can I add the doors sound opening and closing to this script? 1 Answer
How do i make the player to be "Idle" when reaching the mouse position ? 1 Answer