Question by
sparkinfo · Sep 05, 2016 at 09:48 AM ·
c#unity5endless runner
spawning coins in an endless runner. pausing function for a specific time?
so i am making an endless runner game. i have my player running and i can instantiate my coins anywhere up ahead my player through the update function but the problem is that since its in update function it keeps spawning them right after one another. what i want is to spawn a bunch of coins lets say 5-6 at once and then wait for some time lets say 20 seconds before spawning them again.
Comment
Best Answer
Answer by ToothPaste17 · Sep 05, 2016 at 11:34 AM
What you need is a coroutine, which allows you to pause code execution. This should work:
void Start()
{
StartCoroutine(InstantiateCoins());
}
IEnumerator InstantiateCoins()
{
//first, instantiate coins
//then wait 20 seconds
yield return new WaitForSeconds(20);
//then call the method again
StartCoroutine(InstantiateCoins());
}
wow thanks alot i didnt think you could make it loop over and over using Start() like this and not using Update() at all x)
but this will spawn only 1 coin at a time. how to make it spawn a bunch of them at once?