0

So, I started using Unity Photon Engine and RPC very recently. To Summarize, my project is about changing color of a square by using a color picker, but the color of the square should change in all instances of the game, During Run-Time, Master Client can change the color of the square and it changes in all instances(PCs) but, In case of other clients they can neither change the color of their own square nor the color of squares on other instances(PCs). It's like the master client has the permission to change colors but not anybody else.

using System;
using System.Collections;
using System.Collections.Generic;
using Photon.Pun;
using TMPro;
using UnityEngine;
using UnityEngine.Experimental.TerrainAPI;

public class ColorChange : MonoBehaviourPunCallbacks
{
public FlexibleColorPicker colorPickerReference;
private SpriteRenderer sqaureReference;
private PhotonView myPhotonView;
private PhotonView colorPickerReferencePhotonView;
private Color colorReference;
private void Start()
{
    myPhotonView = GetComponent<PhotonView>();
    sqaureReference = GetComponent<SpriteRenderer>();
    colorPickerReferencePhotonView = colorPickerReference.gameObject.GetComponent<PhotonView>();
}

private void Update()
{
    colorReference = colorPickerReference.color;
    
    if (myPhotonView.IsMine)
    {
        sqaureReference.color = colorPickerReference.color;
        myPhotonView.RPC("ColorChangeFunc", RpcTarget.All, colorReference.r, colorReference.g, colorReference.b,
            colorReference.a);
    }
     
    
}

[PunRPC]
void ColorChangeFunc(float r, float g, float b, float a)
{
    Color color = new Color(r, g, b, a);
    sqaureReference.color = color;
}}

Note: The Photon plugin is working as the connection is being established and the lobby is being joined.

I Thank you in advance, All help is appreciated.

  • Whatever you do ... you for sure do not want to call an `RPC` **every frame**! Rather make your code event driven and only apply and send an update when `colorPickerReference.color` really changes! – derHugo Jul 26 '22 at 06:56
  • And then as the color picker has its own `PhotonView` .. are both of these objects owned by the according local player? – derHugo Jul 26 '22 at 06:57
  • Hi @derHugo, I tried your suggestion and I would say, The color transition look a hell of a lot better but the issue still persists, I solved it using Hashtables and Player properties but, I want RPC to work as well, Help me – Aksh Agarwal Jul 27 '22 at 04:34

0 Answers0