The question is answered, right answer was accepted
Play audio once not working
Hi there,
I'm encountering a problem with my audio clip. It will not play at all unlike the others that I've been working with.
Everything else in my script is working and I'm starting to wonder if it is the Update function that overwrites it multiple times.
Anyone with an idea or perhaps solution for this problem? I've been looking around but didn't find anything that would solve it.
Here is the code: using UnityEngine; using System.Collections; using UnityEngine.UI;
public class CountDown : MonoBehaviour {
public float timeRemaining = 119f;
public bool isStarted = false;
private float minutes;
private float seconds;
public AudioSource VoiceFail;
public Gamemaster Master;
public void startTimer ()
{
isStarted = true;
}
void Update()
{
if (!isStarted) return;
timeRemaining -= Time.deltaTime;
minutes = Mathf.Floor (timeRemaining / 60);
seconds = timeRemaining % 60;
if (timeRemaining < 0)
{
minutes = 0;
seconds = 0;
}
GetComponent<Text>().text = minutes.ToString ("0") + ":" + seconds.ToString("00");
if (timeRemaining < 0)
{
StartCoroutine (Fail());
}
}
private IEnumerator Fail ()
{
yield return new WaitForSeconds (0f);
VoiceFail.Play();
yield return new WaitForSeconds (15f);
Master.Gameover(); // GAME OVER AFTER 120 SECONDS AND PLAYER DID NOT FINISH TASKS
yield break;
}
}
Thank you in advance for replying!
Fixed the code myself 2 days ago, but did not find the time to reply on it and close the question.
So here it is. using UnityEngine; using System.Collections; using UnityEngine.UI;
public class CountDown : $$anonymous$$onoBehaviour {
public float timeRemaining = 119f;
public bool isStarted = false;
private float $$anonymous$$utes;
private float seconds;
public AudioSource VoiceFail;
public Gamemaster $$anonymous$$aster;
public void startTimer ()
{
isStarted = true;
}
void Update()
{
if (!isStarted) return;
if (timeRemaining > 0)
timeRemaining -= Time.deltaTime;
if (timeRemaining < 0)
{
$$anonymous$$utes = 0;
seconds = 0;
StartCoroutine (Fail());
timeRemaining = 0;
}
$$anonymous$$utes = $$anonymous$$athf.Floor (timeRemaining / 60);
seconds = timeRemaining % 60;
GetComponent<Text>().text = $$anonymous$$utes.ToString ("0") + ":" + seconds.ToString("00");
}
private IEnumerator Fail ()
{
yield return new WaitForSeconds (0f);
VoiceFail.Play(); // NOT WOR$$anonymous$$ING YET!!!
yield return new WaitForSeconds (15f);
$$anonymous$$aster.Gameover(); // GA$$anonymous$$E OVER AFTER 120 SECONDS AND PLAYER DID NOT FINISH TAS$$anonymous$$S
yield break;
}
}
It was kind of a idiotic error that I made. The code should explain itself, but if your gonna use it and you don't know how, feel free and pm me I'll see if I can explain it further to you.
Follow this Question
Related Questions
audio.play does not work 1 Answer
audio is not playing during IEnumerator 1 Answer
Play a variety of audio source objects from a for loop 2 Answers
Upon hitting Play, my character creates a new Audio Source Component 0 Answers
Audio is cut in script 0 Answers