Question by
unity_oLFfsDJmlThqUw · Jan 07, 2019 at 04:17 PM ·
c#updateaudiosource
Trying to get audiosource to play once
I'm checking if the player is dead in void update and if it is a 3 second countdown starts and I'd like to play an audio clip once in that time
void Update () {
if (player == null)
{
gameOverText.gameObject.SetActive(true);
GetComponent<AudioSource>().Play();
restartTimer -= Time.deltaTime;
if (restartTimer <= 0f)
{
SceneManager.LoadScene("Game");
}
}
}
restart timer is 3f btw. How do I make the clip play only once when this happens?
Comment
Best Answer
Answer by TheHoboCoder · Jan 09, 2019 at 01:26 PM
private bool audioPlayed = false;
void Update () {
if (player == null)
{
gameOverText.gameObject.SetActive(true);
if (!audioPlayed)
{
GetComponent<AudioSource>().Play();
audioPlayed = true;
}
restartTimer -= Time.deltaTime;
if (restartTimer <= 0f)
{
SceneManager.LoadScene("Game");
}
}
}
Thanks a ton I was really struggling to figure out the logic on that one.
Your answer
Follow this Question
Related Questions
Second AudioClip won't play 0 Answers
Start() and Update() execution issue 6 Answers
Vector3.Lerp in update, how stop? 1 Answer
[Got it!]Classes keep spitting out null values (trying to play a looped song) 2 Answers
AudioSource.Play plays every sound at once,AudioSource.Play Plays every source at once 0 Answers