I’m making a multiplayer game and when I client joins while the game is starting, I stop the countdown coroutine. I want it like that to avoid errors and to let the new player vote for the map and game-modes. Should I not use a coroutine?
I wouldn’t. Once start is pressed, set a timer to 10 seconds and count it down.
When players join, either stop the count or reset the count to 10 seconds.
There’s just not a lot of ways this code can go bad:
// zero means don't start, nonzero means start in that amount of time.
float timeToStart;
void Update()
{
if (timeToStart > 0)
{
timeToStart -= Time.deltaTime;
if (timeToStart <= 0)
{
// call the start game here
}
}
}
void ResetCountdown()
{
timeToStart = 10;
}
void StopCountdown()
{
timeToStart = 0;
}