Sync data to different players
Hello! I have buttons in my masterclient an if the host presses that button, other players will see what button the host pressed. I tried PunRPC but it's not working. Does anyone know what to do? here is a partial of the script
I have this button
public void GameObjectValue()
{
gameObjectVote = true; //bool
button1.interactable = false;
button2.interactable = false;
}
and then this PunRPC
[PunRPC]
void SendGameObjectValue()
{
gameObjectValue.text = "Game1";
gameObjectValue.SetActive(true);
}
and then I set it in update to update the data everytime a new player enters the room
void Update()
{
if (button1.interactable == false && button2.interactable == false)
{
photonView.RPC("SendGameObjectValue", RpcTarget.Others);
}
}
Answer by Captain_Pineapple · Oct 20, 2020 at 08:37 AM
man PUN really should rework their RPC tutorial.... i see so many people running into this same issue....
anyway...: First lets try to understand what an RPC is. It is a function that player A and B know of and that one player can call at the other players game instance.
So A can call SendGameObjectValue
to be executed at player B. The only information that you currently send however is that this function is to be executed. You do not transmit any other data yet.
To change that you have to add arguments to that function. Lets assume you have a function
void SendGameObjectValue(bool value1, bool value2)
{
gameObject1.SetActive(value1);
gameObject2.SetActive(value2);
}
then that would be able to receive 2 boolean values and set those values to the active state of the local objects gameObject1
and gameObject2
Then you only have to add actual values to the RPC call:
photonView.RPC("SendGameObjectValue", RpcTarget.Others, true, false);
This will then call SendGameObjectValue
at all other players where this photonView is existent with the arguments value1= true and value2 = false
Hello thank your responding. Do I still put it in the update function or on click button for the host? because when I did this void Update() { if (button1.interactable == false && button2.interactable == false) { photonView.RPC("SendGameObjectValue", RpcTarget.Others, true, false); } }
I get
NullReferenceException: Object reference not set to an instance of an object
Sorry I'm new to photon and some are really confusing. Thank you for your time
oh boy, sorry for reading this really late. In case you have not solved this yet: If you post errors please always add the code it references to. Otherwise this error can mean anything.
Hello its okay thank you for still responding. I was able to remove the error by removing the RPC in update function
Your answer
![](https://koobas.hobune.stream/wayback/20220613014112im_/https://answers.unity.com/themes/thub/images/avi.jpg)