I want to make a team vs team game using mirror in Unity.
This game goes by rounds, so once one team eliminates the members of the other team, the winner team gets one point and when a team get a certain number of points, the game ends.
To select teamm the player selects in a dropdown menu in a previous scene which team wants, and store it in the PlayerPrefs, and then when the player gameobject is instantiated, I get the PlayerPrefs and pass it to the Game Manager.
playerTeam = PlayerPrefs.GetInt("Team");
GameManager.sharedIstance.AddNewPlayer(playerId, playerTeam);`
Then in the GameManager method I update the numberOfPlayers in team
public void AddNewPlayer(int playerID, int team)
{
ActivePlayers.Add(playerID);
//Cuando se añaden los jugadores se incrementa el contador de jugadores en el equipo correspondiente
switch (team)
{
case 0:
numberOfPlayerTeam1++;
break;
case 1:
numberOfPlayerTeam2++;
break;
}
This works as intended and the players are separated in diferent teams so they can play.
At the end I restart the scene to play again but both players now belong to the same team
private NetworkManager Room
{
get
{
if (room != null) { return room; }
return room = NetworkManager.singleton;
}
}
private void RestartRound()
{
Room.ServerChangeScene("InGameScene");
}
This game also uses Playfab and also tried to store there the team info, and get the playerPrefs updated by the GetUserData and SetUserData methods
public void SetUserData()
{
PlayFabClientAPI.UpdateUserData(new UpdateUserDataRequest()
{
Data = new Dictionary<string, string>() {
{"Player", PlayerPrefs.GetInt("Team").ToString()}
}
},
result => Debug.Log("Successfully updated user data"),
error => {
Debug.Log("Got error setting user data Ancestor to Arthur");
Debug.Log(error.GenerateErrorReport());
});
}
public void GetUserData(string myPlayFabeId)
{
PlayFabClientAPI.GetUserData(new GetUserDataRequest()
{
PlayFabId = myPlayFabeId,
Keys = null
}, result => {
Debug.Log("Got user data:");
if (result.Data == null || !result.Data.ContainsKey("Team")) Debug.Log("Team");
else
{
PlayerPrefs.SetInt("Team", Int32.Parse(result.Data["Player"].Value));
}
}, (error) => {
Debug.Log("Got error retrieving user data:");
Debug.Log(error.GenerateErrorReport());
});
}
And then when the Player is instantiated
PlayfabController.sharedInstance.GetUserData(PlayerPrefs.GetString("PlayFabId"));
playerTeam = PlayerPrefs.GetInt("Team");
But the result it's the same. The first round works as intented, but in the next rounds, all player changes to the same team.
Could you tell me how can I persist the team selection data between rounds please?