1) use Invoke
private void BeginRace()
{
Invoke("WaveFlag", 0.5f);
Invoke("Beeps", 1.5f);
Invoke("CrowdBeginsCheer", 2f);
Invoke("CarsStartMoving", 2.2f);
}
2) use coroutine
private void BeginRace()
{
StartCoroutine(RaceSequence());
}
private IEnumerator RaceSequence()
{
yield return new WaitForSeconds(.5f);
WaveFlag();
yield return new WaitForSeconds(1f);
Beeps();
yield return new WaitForSeconds(.5f);
CrowBeginsCheer();
yield return new WaitForSeconds(.2f);
CarsStartMoving();
}
You must master both coroutines and Invoke. Be sure to simply use Invoke when possible. Avoid coroutines when you are just learning Unity. (Advanced essay on coroutines.)
3) "I need to wait until the previous function has ended before executing the following one"
a) EACH OF THOSE FUNCTIONS must be an IEnumerator
private IEnumerator ExplodeCar()
{
..
}
private IEnumerator CrowdReaction()
{
..
}
private IEnumerator WinningCelebration()
{
..
}
b) To call them one after the other, waiting for each to finish
private void Sequence()
{
StartCoroutine(Seq())
}
private IEnumerator Seq()
{
yield return StartCoroutine(ExplodeCar());
yield return StartCoroutine(CrowdReaction());
yield return StartCoroutine(WinningCelebration());
}
Footnotes
If you want to wait until the next frame, use:
yield return null;
if you have a "stack" of things you want to do each frame, just do this
void Update()
{
if (newItem = yourStack.Pop())
newItem();
}
if you have a "stack" of things you want to do waiting for each to finish,
void Start()
{
StartCoroutine(YourStackLoop());
}
private IEnumerator stackLoop()
{
while(true)
{
if (newItem = yourStack.Pop())
yield return StartCoroutine(newItem());
else
yield return new;
}
}
Note that Update and a coroutine are fundamentally the same thing, read and study on this.
Note in the example, use your own usual Push/Pop (or FIFO, or whatever you wish). If unfamiliar, search many QA on here.