- Home /
Unity photon multiplayer teams
Hello, I'm trying to figure out how to create my script for spawning players and assignment to the team. When the player joins, player will be added to the team with less players, if the players are the same player will join the red team. When I tested my script but the players always join the red team, I'd be glad for any help.
const string VERSION = "v0.0.0.2";
//Teams
public int playersOnBlue;
public int playersOnRed;
public Transform[] spawnPointsBlue;
public Transform[] spawnPointsRed;
public Transform point;
void Start()
{
PhotonNetwork.ConnectUsingSettings(VERSION);
}
void OnJoinedLobby()
{
RoomOptions roomOptions = new RoomOptions() { IsVisible = false, MaxPlayers = 4 };
PhotonNetwork.JoinOrCreateRoom("room", roomOptions, TypedLobby.Default);
}
void OnJoinedRoom()
{
if (playersOnBlue >= playersOnRed)
{
GameObject player = PhotonNetwork.Instantiate("PlayerRed", point.position, point.rotation, 0);
photonView.RPC("AddPlayers", PhotonTargets.AllBuffered, 0);
}
else if (playersOnRed >= playersOnBlue)
{
GameObject player = PhotonNetwork.Instantiate("PlayerBlue", point.position, point.rotation, 0);
photonView.RPC("AddPlayers", PhotonTargets.AllBuffered, 1);
}
else if (playersOnRed == playersOnBlue)
{
GameObject player = PhotonNetwork.Instantiate("PlayerRed", point.position, point.rotation, 0);
photonView.RPC("AddPlayers", PhotonTargets.AllBuffered, 0);
}
}
[PunRPC]
void AddPlayers(int blue)
{
if(blue == 1)
{
playersOnBlue++;
} else
{
playersOnRed++;
}
}
Answer by UDN_b7e02a1f-c47d-43ec-877f-367ff4696609 · Apr 23, 2017 at 12:41 PM
Ok i figured it out. I used photon bulit-in teams script. If anyone Interested, here is the script:
const string VERSION = "v0.0.0.2";
public Transform[] spawnPointsBlue;
public Transform[] spawnPointsRed;
void Start()
{
PhotonNetwork.ConnectUsingSettings(VERSION);
}
void OnJoinedLobby()
{
RoomOptions roomOptions = new RoomOptions() { IsVisible = false, MaxPlayers = 4 };
PhotonNetwork.JoinOrCreateRoom("room", roomOptions, TypedLobby.Default);
}
void OnJoinedRoom()
{
if (PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count >= PunTeams.PlayersPerTeam[PunTeams.Team.red].Count)
{
GameObject player = PhotonNetwork.Instantiate("PlayerRed", spawnPointsRed[PunTeams.PlayersPerTeam[PunTeams.Team.red].Count].position, spawnPointsRed[PunTeams.PlayersPerTeam[PunTeams.Team.red].Count].rotation, 0);
PhotonNetwork.player.SetTeam(PunTeams.Team.red);
}
else if (PunTeams.PlayersPerTeam[PunTeams.Team.red].Count >= PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count)
{
GameObject player = PhotonNetwork.Instantiate("PlayerBlue", spawnPointsBlue[PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count].position, spawnPointsBlue[PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count].rotation, 0);
PhotonNetwork.player.SetTeam(PunTeams.Team.blue);
}
else if (PunTeams.PlayersPerTeam[PunTeams.Team.red].Count == PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count)
{
GameObject player = PhotonNetwork.Instantiate("PlayerRed", spawnPointsRed[PunTeams.PlayersPerTeam[PunTeams.Team.red].Count].position, spawnPointsRed[PunTeams.PlayersPerTeam[PunTeams.Team.red].Count].rotation, 0);
PhotonNetwork.player.SetTeam(PunTeams.Team.red);
}
}
Hello, I tried your solution, but when it tries to do this comparison :
if (PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count <= PunTeams.PlayersPerTeam[PunTeams.Team.red].Count)
it throw an error :
NullReferenceException: Object reference not set to an instance of an object
Did you have this issue ?
Answer by ManuGT · Apr 06, 2018 at 06:26 PM
the error is because they have to assign the photon script "PunTeams" to the gameobject
Your answer
Follow this Question
Related Questions
Player Instantiate won't work 2 Answers
Spawn players in unique places (Photon networking) Object sync problem for entering players. 0 Answers
Photon Instantiate 2 Answers
Is possible to spawn 2d image in photon networking ? 1 Answer
Spawning troubles 2 Answers