0
votes

I have created a scene with a single dice where players will click on the dice turn by turn which will change the color of the dice. First I have created a room and a player joined the room and entered the game scene. Till here everything works fine. But when player 1(host) clicks on the dice, player 2 doesn't see the dice moving. I have added the photon transform view in the inspector and here is my code:

void Update()
    {
        turnBaseGame();
    }
    
    public void turnBaseGame()
    {
         PhotonView photonView = PhotonView.Get(this);
         photonView.RPC("OnMouseDown", RpcTarget.All);
    }
    
    [PunRPC]
    public void OnMouseDown()
    {
       if (Input.GetMouseButtonDown(0))
       {
           // color change
       }
    }

I am clicking on the dice using OnMouseDown function but both player's dice in the game scenes are not synchronizing. I want the dice to show on both screens whenever a player clicks on the dice, the color changes and it should show to other player's screen. Then in the next turn another player will do the same. Please help me to solve this issue. Thanks

2

2 Answers

2
votes

You should separate methods for localPlayer and remote player.

Check photonView.IsMine, if isMine == true call method locally and notify remotes with RpcTargets.All

    void OnMouseDown() 
    {
      if(photonView.isMine)
      {
         rollDice(); // call local
         //notify other players about your turn
         photonView.RPC("rollDice", RpcTarget.Others); 
       }
    }
   
   [PunRPC]
   void rollDice() 
   {
      //Your logic here
   }

So if you play locally changes will appear correctly on each player device.

!!!DO NOT!!! mark EventFunctions like Update/Start/OnMouseDown as RPC! Make another method for your tasks...

Sorry for my English...

0
votes

photonView.RPC("rollDice", RpcTarget.Others);

Instead of RpcTarget.Others use RpcTarget.All for reflecting the dice rolling animation and change in player and other players. RpcTarget.Others will show dice rolling in another players not in our device.