- Home /
Why is this method being called multiple times?
I use a script that calls a few IEnumerators in the Start method only on the server. In the following IEnumerator it simply checks for the "last man standing" in a battle:
IEnumerator CheckCurrentBattle()
{
yield return new WaitForSeconds(0.1f);
if (currGameState == CurrentGameState.Battle)
{
int alivePlayerCount = 0;
GameObject winningPlayer = null;
GameObject[] players = getPlayerGameObjects();
foreach (GameObject player in players)
{
if (!player.GetComponent<PlayerStats>().isDead)
{
alivePlayerCount++;
winningPlayer = player;
}
}
if (alivePlayerCount == 1 || alivePlayerCount == 0)
{
if (alivePlayerCount == 1)
{
------>winningPlayer.GetComponent<Player>().awardPlayerWin();<------
}
else
RpcServerMessage("Draw!", false);
currGameState = CurrentGameState.BattleEnding;
StartCoroutine(EndCurrentBattle());
}
}
StartCoroutine("CheckCurrentBattle");
}
However, the server method I call where the <---arrows---> are put are getting called multiple times. For example when 3 players are connected, this method will be called 3 times. This method is just a simple server method that increases some [SyncVar] variables:
[Server]
public void awardPlayerWin()
{
playerStats.playerGold += 5;
playerStats.playerScore += 1;
}
Why is this method being called multiple times instead of just one time on the server?
Yeah that's the point. It repeats itself every 0.1 second, but that's not the problem here.
Is it being triggered by clients to run on server? So three clients trigger it three times. Does it also seem to run on clients or just the server?
Is this being called in the Player.Start() method? If so, you are starting it for each player you spawn.