Hi guyz,
I’ve been searching some countdown methods with c# in forum but cant find complete one.
And mine is complete now. Small thing but wanted to share…
Hope help someone… ![]()
In my usage i attached that to my vehicle and and checked ‘canStart’ variable before apply throttle to wheels.
using UnityEngine;
using System.Collections;
public class CountDownBeforeStart: MonoBehaviour
{
public bool canStart = false;
public AudioSource AudioPlayer;
public AudioClip BipCount;
public AudioClip BipStart;
public float CountDownFrom = 5;
private bool _isCounting = false;
void Start()
{
if (CountDownFrom > 0)
{
CountDown();
}
}
void CountDown()
{
if (!_isCounting)
{
StartCoroutine(Wait());
}
}
IEnumerator Wait()
{
_isCounting = true;
for (float i = CountDownFrom; i >= 0; i--)
{
Debug.Log(i);
if (i == 3||i== 2||i== 1)
PlayAudio(BipCount);
else if (i== 0)
{
PlayAudio(BipStart);
canStart= true;
}
yield return new WaitForSeconds(1);
}
_isCounting = false;
}
void PlayAudio(AudioClip Clip)
{
AudioPlayer.clip = Clip;
AudioPlayer.loop = false;
AudioPlayer.Play();
Debug.Log("Playing " + Clip.name);
}
}