Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Third Party Photon Pun 2 RPC Issue

Discussion in 'Multiplayer' started by CladCobra, Jul 7, 2023.

  1. CladCobra

    CladCobra

    Joined:
    May 26, 2021
    Posts:
    3
    I've been struggling with this problem in my multiplayer game for a few days now. I'm using this line to instantiate my player object:
    GameObject newPlayer = PhotonNetwork.Instantiate(playerPrefab.name, Vector3.zero, Quaternion.identity);

    The master client instantiates an object over the network called NetworkManager that finds the master client in the start function using this:
    Code (CSharp):
    1. foreach (PlayerController playerController in FindObjectsOfType<PlayerController>()) {
    2.  
    3.        if (playerController.photonView.OwnerActorNr == PhotonNetwork.MasterClient.ActorNumber) {
    4.  
    5.                 masterController = playerController;
    6.  
    7.        }
    8. }
    When the game is ready to start I call an RPC in my PlayerController script which is on the object to start the game for each player:

    Code (CSharp):
    1. [PunRPC]
    2.     public void StartGameRPC() {
    3.  
    4.         if (photonView.IsMine) {
    5.  
    6.             //logic goes here
    7.  
    8.         }
    9.     }
    The RPC successfully runs for every client connected to the room but for some reason the photonView in my RPC is always the master client's photonView no matter which client its being run for. I've been trying different things but I can't seem to figure out why.
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    You haven't shown how you are calling the RPC
     
    Last edited: Jul 7, 2023
  3. CladCobra

    CladCobra

    Joined:
    May 26, 2021
    Posts:
    3
    I call it in the Update method in the Network Manager script:
    Code (CSharp):
    1. private void Update() {
    2.  
    3.         if (masterController.photonView.IsMine && PhotonNetwork.PlayerList.Length == gameManager.GetMaxPlayers() && gameManager.GetGameState() == GameManager.GameState.Waiting) {
    4.  
    5.             masterController.photonView.RPC("StartGameRPC", RpcTarget.All);
    6.  
    7.         }
    8.     }
     
  4. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    583
    I do not have checked the main problem but man ... never send RPC in update method...
    If you have a high latency, you gonna send it every game loop until you get the correct GameState...
     
  5. CladCobra

    CladCobra

    Joined:
    May 26, 2021
    Posts:
    3
    Yea I realized that after I posted this and used a boolean check to make sure the RPC is only called once, is there still a problem with this method?