Pun 2 Simple RPC

So im just learning Photon and i cant quite figure out RPC’s. I wanted to make a super simple RPC to help me understand it better but i cant seem to be able to. I wanted to make a simple scene where when you hit a button it changes the colour of a cube and that everyone is able to see the colour change.

Here’s my code

using System.Collections;
using System.Collections.Generic;
using Photon.Pun;
using UnityEngine;

public class Test : MonoBehaviourPun
{

    public GameObject cube;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (photonView.IsMine)
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                changeColour();
            }

        }
       
    }

    [PunRPC]
    void changeColour()
    {
        float r = Random.Range(0f, 1f);
        float g = Random.Range(0f, 1f);
        float b = Random.Range(0f, 1f);

        print(r + " " + g + " " + b);
        cube.GetComponent<Renderer>().material.color = new Color(r, g, b, 1f);

        photonView.RPC("changeColour", RpcTarget.AllBuffered, cube.GetComponent<Renderer>().material.color);
    }
}

Hey there,

well your current code does primarily not work because you tell photon in the RPC(…) that it should call the function “changeColour” with an argument of type Color. Which does not exist since your function does not accept an argument. Still just adding this argument is a bad idea since it basically would cause infinit messages if that was possible.
Your current flow is like this:

you press E and thus call changeColour

changeColour does change your color locally and then triggers a message for everyone which basically says: “Go on and call the method changeColour on the photonview this message belongs to”

which in return will trigger more messages from everyone to everyone

in general: A RPC should never ever call itself as a RPC

so what you want to do is basically to change your code to the following:

using System.Collections;
using System.Collections.Generic;
using Photon.Pun;
using UnityEngine;

public class Test : MonoBehaviourPun
{

 public GameObject cube;

 void Update()
 {
     if (photonView.IsMine)
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
            float r = Random.Range(0f, 1f);
            float g = Random.Range(0f, 1f);
            float b = Random.Range(0f, 1f);

             print(r + " " + g + " " + b);
             photonView.RPC("changeColour", RpcTarget.AllBuffered, r,g,b);
         }
     }
 }

 [PunRPC]
 void changeColour(float r, float g, float b)
 {
     cube.GetComponent<Renderer>().material.color = new Color(r, g, b, 1f);
 }
}

if something is still unclear let me know in the comments,