Having trouble using JoinTeam() method

Options
Nyte
Nyte

So when I call a simple command like PhotonNetwork.LocalPlayer.JoinTeam(), for the person who creates the room it works fine. After that who ever joins gets this error

Caught exception in OnEvent() for event code 255: System.NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Debug:LogError(Object)
Photon.Realtime.LoadBalancingClient:DebugReturn(DebugLevel, String) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:2564)

Why does this happen? I'm just calling this, and this only in the OnJoinedRoom() Method. I'll appreciate all help, thanks!

Best Answer

  • JohnTube
    JohnTube ✭✭✭✭✭
    Answer ✓
    Options

    Hi @Nyte,

    Please update to the latest PUN 2 to be able to see the exact stack trace.

    We need to the exact line of code where the NullReferenceException is thrown.

    I also have checked the video, you do not use the exact same code as the one shown there.

Answers

  • JohnTube
    JohnTube ✭✭✭✭✭
    Options

    Hi @Nyte,

    Thank you for choosing Photon!

    This is probably a NullReferenceException in OnJoinedRoom callback.

    Please update to the latest PUN 2 to see better correct stack trace.

  • Nyte
    Options

    Hey @JohnTube,

    Thanks for your reply!

    I don't think it was a NullReferenceException in OnJoinedRoom Callback because if I remove that line, I don't get the error. Either way I found this tutorial

    And I implemented the teams function the way he did, and I stopped getting the error. Though I decided to watch it all, when he goes over creating teams etc, and it turns out that was exactly what I'm tryna do. So I decided to watch it all. I tested it. And I get the same error as I did before, why?

    Script;

    private void AutoAssignPlayerToTeam(Player player, GameModes gameMode)
        {
            foreach (PhotonTeam team in _roomTeams)
            {
                int teamPlayerCount = PhotonTeamsManager.Instance.GetTeamMembersCount(team.Code);
    
    
                if (teamPlayerCount < gameMode.TeamSize)
                {
                    Debug.Log($"Auto assigned {player.NickName} to {team.Name}");
                    if (player.GetPhotonTeam() == null)
                    {
                        player.JoinTeam(team.Code);
                    }
                    else if (player.GetPhotonTeam().Code != team.Code)
                    {
                        player.SwitchTeam(team.Code);
                    }
                    break;
                }
            }
        }
    
    
        private void CreateTeams(GameModes gameMode)
        {
            _teamSize = gameMode.TeamSize;
            int numberOfTeams = gameMode.MaxPlayers;
            if (gameMode.HasTeams)
            {
                numberOfTeams = gameMode.MaxPlayers / gameMode.TeamSize;
            }
    
    
            for (int i = 1; i <= numberOfTeams; i++)
            {
                _roomTeams.Add(new PhotonTeam
                {
                    Name = $"Team {i}",
                    Code = (byte)i
                });
            }
        }
        private void JoinTeam(GameModes gameMode)
        {
            CreateTeams(gameMode);
    
            AutoAssignPlayerToTeam(PhotonNetwork.LocalPlayer, gameMode);
        }
    

        I call the JoinTeam method in the OnJoinedRoom Callback, and I get the same error as before when someone joins, lets say I create the room, then my friend joins, my friend will get the error, but then if my friend creates the room and I join, it works just as intended to and neither get the error, but thats just for 2 people, it everyone gets the error when there's more then 2 people except the Maser Client.

  • JohnTube
    JohnTube ✭✭✭✭✭
    Answer ✓
    Options

    Hi @Nyte,

    Please update to the latest PUN 2 to be able to see the exact stack trace.

    We need to the exact line of code where the NullReferenceException is thrown.

    I also have checked the video, you do not use the exact same code as the one shown there.

  • Nyte
    Nyte
    edited December 2021
    Options

    Hey @JohnTube

    Thanks for your reply!

    I thought I did update to the latest version, I'll check again.

    And about the same exact code, do you see something different in those 2 functions? or the way he called them, if it's the way he called it, it doesn't really matter if I call it from another script, use an action to call it, or call it directly in the OnJoinedRoom() callback, does it?

    Either way me getting which line the error is on will make it really simple for me to fix it!! So I really appreciate that!

    The error is thrown here by the way ( teamSize = gameMode.TeamSize;)

  • Nyte
    Options

    Hey @JohnTube

    Thanks for your help, I was able to solve the issue, though I still do have 1 more question, how would I go about with team spawning, so like lets say I have 2 teams, I instantiate one team on one side, and the other team on another side. What method would I use

    Thanks!