PunTeam how to get player tem red or blue count ?

i use this line Debug.Log( PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count)
and recive null reference, i need help =/

Answers

  • Hi @Valakinhas,

    PunTeams.PlayersPerTeam is a dictionary containing the Team itself as key and a list of players, which have joined that team, as value. To get the number of for a certain team, you have to access the Count property of the value of a certain Key-Value-Pair. For example for the blue team: PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count. Please make sure to add the PunTeams script as a component to a game object before trying to access PlayersPerTeam.
  • Not work, this is a bug =\
  • Can you share some more details why you are thinking that this is bug? It has been working in my test so far.
  • my player have

    Can you share some more details why you are thinking that this is bug? It has been working in my test so far.

    void OnJoinedRoom()
    {



    for (int i = 0; i < PhotonNetwork.room.PlayerCount; i++)
    {
    var team = PunTeams.Team.blue;

    if (i >= count)
    {
    team = PunTeams.Team.red;

    }

    PhotonNetwork.playerList[i].SetTeam(team);

    Debug.Log(PunTeams.PlayersPerTeam[PunTeams.Team.blue].Count); // null reference
    }

  • Have you attached the PunTeams script as a component to any GameObject in the same scene where you try to access it from code? It won't work if the component is not attached to an existing GameObject.

    A hint to the code snippets: each client who joins the room, will probably assign a new team to each client in the room. If this is wanted behaviour, it's fine. Otherwise you can try getting rid of the for-loop and only assign a team to the client who joined the room (OnJoinedRoom callback).
  • Have you attached the PunTeams script as a component to any GameObject in the same scene where you try to access it from code? It won't work if the component is not attached to an existing GameObject.

    A hint to the code snippets: each client who joins the room, will probably assign a new team to each client in the room. If this is wanted behaviour, it's fine. Otherwise you can try getting rid of the for-loop and only assign a team to the client who joined the room (OnJoinedRoom callback).

    have GameObject and Photonview
Sign In or Register to comment.