In my game I have several player characters switchable using the tab key. During the update on the Player class I will check (if the current Player class is the active one) if the Tab key is released. If it is, then the game should switch to the next player character
Unfortunately this does not work as expected as sometimes the tab key is released several times
Here’s the interesting code:
Player:
public void Update()
{
if (IsActivePlayerInstance)
{
HandleInput();
}
}
private void HandleInput()
{
if (Input.GetKeyUp(KeyCode.Tab))
{
GameObject manager = GameObject.FindGameObjectWithTag("Manager");
LevelManager lev = manager.GetComponent<LevelManager>();
lev.SetActivePlayerNext();
}
}
LevelManager:
public void SetActivePlayerNext()
{
int nextPlayerId = -1;
foreach (Player player in PlayerInstances)
{
if (player.IsActivePlayerInstance)
{
nextPlayerId = player.PlayerId + 1;
}
}
if (nextPlayerId > PlayerInstances.Count)
{
nextPlayerId = 1;
}
SetActivePlayerInstance(nextPlayerId);
}
public void SetActivePlayerInstance(int playerId)
{
foreach (Player player in PlayerInstances)
{
if (player.PlayerId == playerId)
{
player.IsActivePlayerInstance = true;
}
else
{
player.IsActivePlayerInstance = false;
}
}
}
In the player class there is a property IsActivePlayerInstance, which is only changed in the above LevelManager methods
Under correct circumstances it should switch between the player characters like this:
Player1 → Player2 → Player3 → Player1…
Unfortunately sometimes it will do like this:
Player1 → Player2 → Player3 → Player2 → Player3…
Or when I just added a few more players:
Player1 → Player3 → Player5 → Player1 → Player3…
I can replicate the same situation if I don’t change playerId on the player characters in the scene. But if I e.g. change the id of player2 to player3 and player3 to player2 it will a completely random result that will be repeated until I change the playerids again.
What I mean is this:
I had 5 characters in the scene, played 5 games and the tab result was this:
Player1 → Player2 → Player3 → Player4 → Player5 → Player2 → Player3 (always skipping player1)
Then I changed the id of Player1 to 5 and the id of player5 to 1 and played 5 games. All games gave this result now:
Player1 → Player3 → Player5 → Player1 → Player3 → Player5 → Player1 (always skipping player2 and player4)
Anybody who has any idea on what the problem could be here?
All player characters are from the same prefab, only changing the playerid