[PUN] How do I use RPCs the right way?
Hello everyone.
This is what I'm trying to do:
User spawns a GameObject
A popup window with an InputField appears. The user enters a name and presses the "ok" button.
The text from the InputField is added to the GameObject.
My problem right now is that the text is not synced between users.
I know that I have to use an RPC. But I have a hard time understanding how to use an RPC correctly.
For example: Which method should receive the [PunRPC] attribute? Is it the method that takes the text from the InputField and adds it to the GameObject? But where would I call this method from then?
Best regards
Answer by Captain_Pineapple · Jul 03, 2020 at 07:01 AM
hey there and thank you for posting a comprehensive question.
basically what you need is 2 functions. First one where you do the following:
public void OnTextConfirmed(string text)
{
//call the RPC for *all* connected players including yourself:
photonView.RPC("updateText", RpcTarget.All, text);
//all other stuff that needs to be done after confirming the text can also go in here.
}
this should be the function that is called from your input field when you confirm the text. Then you only need the RPC:
[PunRPC]
private void updateText(string Text)
{
theGameobjectYouWantToUpdate.name = Text;
}
The latter assumes that you already know the gameobject when the function is called or you have to "search" for the reference in that updateText function. That is up to you.
Hope that clears it up, in case you don't understand something let me know. Cheers.
Your answer
![](https://koobas.hobune.stream/wayback/20220613002236im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity3D photon. bool resets. 0 Answers
OnTriggerEnter worked only on master client 0 Answers
PhotonUnity player move object problem ! 1 Answer
PhotonPun RPC functions Doesnt Work For Both the Players 0 Answers