I have 4 players with their own manager script, I want them to have some kind of respawn timer of their own when they die.
So the game manager script is calling this function every frame to check if any of the player registered into the game is dead.
GameManager.CheckPlayerDeath()
void CheckPlayerDeath()
{
for (int i = 0; i < player.Length; i++)
{
if(player[i].PlayerDeath2())
{
if (player[i].playerDeathConfirmed)
return;
player[i].playerDeathConfirmed = true;
if (!player[i].coroutineIsRunning)
StartCoroutine(player[i].Respawn2(respawnTimer));
}
}
}
And this is the coroutine that the player manager has.
PlayerManager.Respawn2
public IEnumerator Respawn2(float respawnTimer)
{
coroutineIsRunning = true;
DisablePlayerControl2();
playerMesh.enabled = false;
yield return new WaitForSeconds(respawnTimer);
playerDeathConfirmed = false;
ResetPlayerControl2();
ResetRespawn2();
playerMesh.enabled = true;
EnablePlayerControl2();
coroutineIsRunning = false;
}
The problem is everytime player 1 dies (indexed 0 in the player manager array) it seems the other player (indexed 1-3) can’t die before player 1’s coroutine is finished. I reckon it’s happening because I put the coroutine inside the loop so it’s waiting for the current loop coroutine to finish then check for the second loop.
If so, how do I get around it and make each player has their own respawn timer?