I'm trying to make a script that will play a sound when the Round Timer hits 3, until it has reached 0.. A voice speaking "3...2...1...", but my script won't work the way I want it to.. The first sound will play.. 58 times in a row... How can this be fixed? I guess it's my IEnumerator StartCountdown() function that's broken? Here it is: Code (CSharp): using UnityEngine; using UnityEngine.UI; using System.Collections; [RequireComponent(typeof(AudioSource))] public class RoundTimer : MonoBehaviour { public static RoundTimer rt; public Text textElement; public int countDownSeconds = 120; public int freezeTime = 4; public AudioClip roundsUpSound; public AudioClip[] countdownSound; [HideInInspector] public float startTime; private int restSeconds; private int roundedRestSeconds; private int displaySeconds; private int displayMinutes; private bool timesUp = false; private bool updateTime = true; private bool startCount = false; void Awake() { startTime = Time.time; } void Update() { float guiTime = Time.time - startTime; restSeconds = countDownSeconds - ((int)guiTime); roundedRestSeconds = Mathf.CeilToInt (restSeconds); displaySeconds = roundedRestSeconds % 60; displayMinutes = roundedRestSeconds / 60; if (restSeconds == 0) { timesUp = true; startCount = false; StartCoroutine (RoundsUp ()); } else { timesUp = false; } if (restSeconds == 3) { startCount = true; StartCoroutine (StartCountdown ()); } if (!timesUp && updateTime) UpdateTime (); } void UpdateTime() { if (SpawnManager.spawn.freeze) { startTime = Time.time; textElement.color = SpawnManager.spawn.freezeColor; textElement.text = string.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); } else { textElement.color = SpawnManager.spawn.normalColor; textElement.text = string.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); } } IEnumerator StartCountdown() { if (startCount) { Debug.Log(restSeconds); if(restSeconds == 3) { SpawnManager.spawn.GetComponent<AudioSource> ().PlayOneShot (countdownSound [0]); } else if(restSeconds == 2) { SpawnManager.spawn.GetComponent<AudioSource> ().PlayOneShot (countdownSound [1]); } else if(restSeconds == 1) { SpawnManager.spawn.GetComponent<AudioSource> ().PlayOneShot (countdownSound [2]); } else { startCount = false; } } yield return new WaitForSeconds(0f); UpdateTime (); } IEnumerator RoundsUp() { startTime = Time.time; textElement.text = "00:00"; updateTime = false; if (timesUp) { GetComponent<AudioSource> ().PlayOneShot (roundsUpSound); timesUp = false; yield return new WaitForSeconds ((float)freezeTime); updateTime = true; SpawnManager.spawn.SpawnPlayer (); startTime = Time.time; } } }
Why so complicated? Code (csharp): void Start(){ StartCoroutine(PlaySoundEvery(1.0f, 3)); } IEnumerator PlaySoundEvery(float t, int times){ for(int i=0;i<times;i++){ SpawnManager.spawn.GetComponent<AudioSource>().PlayOneShot(countdownSound [i]); yield return new WaitForSeconds(t); } GetComponent<AudioSource>().PlayOneShot(roundsUpSound); } That should be it.
I'd just do something simple like Edit: oops got Ninja'd by Proandrius there - not suggesting my method is better than his Code (CSharp): float timer = 4.0f; int countdown = 3; public AudioSource[] countdownAudioArray; void Update() { timer -= Time.deltaTime; if (timer > 0.0f) { if (timer < (float)countdown) { countdownAudioArray[countdown].Play(); countdown--; } } else { Debug.Log("Times Up"); } }
Thanks.. But still the sound clips are playing 58 times in a row... This line is called 58 times according to the debugger: Code (CSharp): if (restSeconds == 3) { startCount = true; StartCoroutine (StartCountdown (1.0f, 3)); } I don't want in the Start function as you suggested...
Yeah.. solved it by moving it to this section: Code (CSharp): if (restSeconds == 0) { timesUp = true; startCount = false; StartCoroutine (RoundsUp ()); } else if (restSeconds == 3 && !startCount) { StartCoroutine (StartCountdown (1.0f, 3)); } else { timesUp = false; } ...and it worked.. thanks everyone for the help..
Code (CSharp): int timer; void StartCountDown(){ timer = 3; InvokeRepeating("CountDownTick", 1.0f, 1.0f); } void CountDownTick(){ //Play Sound Here timer--; if(timer<=0){ CancelInvoke("CountDownTick"); } }