Why do some buffered RPCs show up sometimes for new players?

For my game, players connect directly to a room where they see three buttons. Each player clicks on the button that represents the character they want to play as. When a player clicks on a button, it disables that button for the other players so no one can play as the same character.

This works fine if all players join the room before anyone clicks a button. However, if two players are in the room and each select a character, then the third player joins, only one of the button is disabled and there is an error
"NullReferenceException: Object reference not set to an instance of an object
DisableButtonFOrAllPlayers.RPC_DisableButton () (at Assets/Tree Axe Fire/Scripts/DisableButtonFOrAllPlayers.cs:25)"

When a button is clicked, this method is called:
public void PyroSelected()
    {
        characterType = CharacterType.Pyro;
        SetSelectedColor(pyroButton);
        pyroButton.GetComponent<DisableButtonFOrAllPlayers>().CallDisableButtonRPC();
        planterButton.interactable = false;
        chopperButton.interactable = false;
        CheckIfAllPlayersReady();
    }

The above code is on an empty Game Object with a PhotonView. However, it calls a script attached directly to the button (also with a PhotonView) that needs to be disabled:
public class DisableButtonFOrAllPlayers : MonoBehaviourPun
{
    Button button;

    private void Start()
    {
        button = this.GetComponent<Button>();
    }

    public void CallDisableButtonRPC()
    {
        photonView.RPC("RPC_DisableButton", RpcTarget.AllBuffered);
    }

    [PunRPC]
    void RPC_DisableButton()
    {
        button.interactable = false;
    }
}

The target is AllBuffered but in practice, the first click is lost if all three players are not connected. However, the second button click seems to get sent to the new player.

Best Answer

  • JohnTubeJohnTube mod
    Accepted Answer
    Hi @Techno_Babel,

    Thank you for choosing Photon!

    There are three different issues:

    1. The NullReferenceException. Probably the RPC RPC_DisableButton is received & respective method is called before Start is called and button is assigned. Try replacing Start with Awake or setting button in RPC_DisableButton.
    2. The weird behaviour of buffered RPC sometimes received sometimes not or not received by everyone.
    3. What you try to do (unique character selection per player), IMO, is better implemented using room or actor properties + CAS.

Answers

  • JohnTubeJohnTube mod
    Accepted Answer
    Hi @Techno_Babel,

    Thank you for choosing Photon!

    There are three different issues:

    1. The NullReferenceException. Probably the RPC RPC_DisableButton is received & respective method is called before Start is called and button is assigned. Try replacing Start with Awake or setting button in RPC_DisableButton.
    2. The weird behaviour of buffered RPC sometimes received sometimes not or not received by everyone.
    3. What you try to do (unique character selection per player), IMO, is better implemented using room or actor properties + CAS.
  • Thanks, JohnTube mod! Suggestion #1 worked.
    [PunRPC]
        void RPC_DisableButton()
        {
            button = this.GetComponent<Button>();
            button.interactable = false;
        }
    
Sign In or Register to comment.