- Home /
audio.PlayOneShot() + Update() = mess?
Hi.
I've been having trouble getting audio.PlayOneShot() to play only once, and not overlap with the same sound each frame, cause Update() is getting called each frame. The thing I want to do is to play a sound once a boolean is true.
Things I've tried:
I created a new boolean variable, and deactivated it after the audio.PlayOneShot() line (if the boolean is true, play a sound, after that line, deactivate it)
I created a boolean function, and play the sound after the "return true;" line
Call it in another (regular void) function (same result, the Update() function is still calling the newly created function each frame),
I have also tried to Invoke() that function.
How could I play a sound when the boolean mentioned in the beginning is turned true?
I'm not sure why but the code you posted here DESTRU$$anonymous$$TORR has the initial bool set to FALS$$anonymous$$ I tried using your code and it didn't work. I'm new to Unity and scripting so I couldn't get your code to work until I set the initial bool to TRU$$anonymous$$ Can someone explain this? This is what is working for me currently in my game:
AudioSource laserAudio;
bool audioEnabled = true;
void Awake () {
laserAudio = GetComponent<AudioSource>();
}
void Update () {
if (Input.GetButton ("Fire1")) {
if(audioEnabled)
{
laserAudio.Play ();
audioEnabled=false;
}
} else {
laserAudio.Stop ();
audioEnabled=true;
}
}
The only difference in my example is that I am not calling the sound once because it's a continuous loop while holding left-click and then I reset the bool to true once I release left-click.
Answer by DESTRUKTORR · Aug 14, 2012 at 02:48 PM
C# version:
bool playAudio=false;
void Update()
{
if(playAudio)
{
audio.PlayOneShot();
playAudio=false;
}
}
Javascript version:
var playAudio = false;
function Update()
{
if(playAudio)
{
audio.PlayOneShot();
playAudio=false;
}
}
Just add the script that isn't in your file to the appropriate areas.
Looks like when I first tried this, I probably did something wrong. Thanks! Works!
so you had the answer all along Luka$$anonymous$$otar....
@TheVectorHunter Well yeah... If you read the question, you'll see that I mentioned this in it. LOL, I've seen this answer, and I thought: oh well, I guess I'll try it again... ;)
I wasn't sure if this was what you meant, but I knew it should work. I'm glad you got it running :) good luck in your future endeavors!
I'm confused. How would this solution work? Surely the lines inside the 'if' statement would never be called because playAudio would never be set to true?
The question has been posted since 2012, so it's very old(even PlayOneShoot method have been changed), I don't know if it was working at that point in time, but logically it doesn't seem to work. Anyway incase someone is looking for a working code :
public AudioSource audio;
public AudioClip clip; // make sure to have a reference in the inspector window
bool playing; // A private boolean with default value set to false
void Start()
{
this.audio.clip = clip;
}
private void Update()
{
if (!this.playing)
{
audio.PlayOneShot(clip);
StartCoroutine(resetPlayingStatus(this.clip.length)); // Starting a coroutine with a float value of clip length
}
this.playing = true; // set playing to true after the clip first playing
}
IEnumerator resetPlayingStatus (float clipLength)
{
yield return new WaitForSeconds(clipLength); // Make the coroutine waits until the clip is finished
this.playing = false; // set playing to false, so the clip plays again
}
Make sure to include using System.Collections;
in your script.
Your answer
Follow this Question
Related Questions
Rigidbody.AddForceAtPosition & functions 1 Answer
Im a bit confused on a simple script 1 Answer
Aiming with function "Update" not working 0 Answers
Update() doesnt call method or modify values 1 Answer
Update not being called 1 Answer