Problem with Text Synchronisation via RPC Call

Options
Hello,

I have a game where I want to instantiate several instances of the same Prefab and make it possible to give them individual names after instantiation and make the Names visible directly above the object.

I attached a script to the Prefab where I want to control the naming and also synchronize the text over the clients via a RPC call. Unfortunately it seems, that I did not fully understand how it works.

I get the following error:
RPC method 'SyncObjectName' found on object with PhotonView 1002 but has wrong parameters. Implement as 'SyncObjectName(String, String)'. PhotonMessageInfo is optional as final parameter.

and this is the (relevant part of the) Code:
(The class is called "DragObject" becaus I use it also to drag the object around in the level)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;


public class DragObject : MonoBehaviourPunCallbacks
{

    public InputField ObjectNameInput;
    public Text ObjectName;

    private PhotonView PV;

    private void Start()
    {
        PV = GetComponent<PhotonView>();
    }

    private void Update()
    {
        if (PV.IsMine)
        {
            PV.RPC("SyncObjectName", RpcTarget.AllBuffered,ObjectName.text,ObjectNameInput.text);
        }
    }

    [PunRPC]
    void SyncObjectName()
    {
        ObjectName.text = ObjectNameInput.text;
    }
}

Why are the parameters in the RPC method wrong? What do I have to do to make it work?

Comments

  • void SyncObjectName() must have at least two string as input to matching your PV.RPC("SyncObjectName") call.
    try SyncObjectName(string txt1, string txt2, PhotonMessageInfo pmInfo) or SyncObjectName(string txt1, string txt2)
  • Tobias
    Options
    Part of the problem will be that you send a RPC every single frame. This is going to kill your network in no time.
    Also, you use AllBuffered, which will break the connections of the other players and anyone joining.

    Don't send in each frame!

    The RPC method has no parameter, so it won't accept the way you call it (with a string parameter as last one). Make it SyncObjectName(string receivedText).

    Last but not least: Read the docs.

  • Dufffit
    Options
    Tobias wrote: »
    Part of the problem will be that you send a RPC every single frame. This is going to kill your network in no time.
    Also, you use AllBuffered, which will break the connections of the other players and anyone joining.

    Don't send in each frame!

    The RPC method has no parameter, so it won't accept the way you call it (with a string parameter as last one). Make it SyncObjectName(string receivedText).

    Last but not least: Read the docs.

    Thank you for your helpful comments! Why will "AllBuffered" break the connections? I thought "AllBuffered" sends the information also to clients who join later. In principle this is what I want. If someone joins later on, they should also see the object and its current name.

    I changed the code a little bit and implemented the parameters correctly (I hope). Also it is not called every frame, only when "return" was pressed (which deactivates also the Inputfield und thus ends the process of changing the name).

    I also put the whole thing to a new script which is attached to the "Text" Object, which is a child of the Prefab that should get a name.
    The script also works for the local client. I can change the Name via the Inputfield and also in the console I get not Error (It shows me: "Sending RPC "SyncObjectName" to target: All or player:." and "Received RPC: SyncObjectName")

    But still the new Name does not appear on other clients. Is there something wrong with the PhotonView Components? I attached a PhotonView Component the the Text child object. This is the component which should be found in the Start-function, right?
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using Photon.Pun;
    using Photon.Realtime;
    
    public class SyncName : MonoBehaviourPunCallbacks
    {
        public InputField ObjectNameInput;
        public Text ObjectName;
    
        private PhotonView PV;
        
        void Start()
        {
            PV = GetComponent<PhotonView>();
            ObjectName.text = ObjectNameInput.text;
        }
    
        
        void Update()
        {
            if (Input.GetKeyDown("return"))
            {
                ObjectNameInput.gameObject.SetActive(false);
                ObjectName.text = ObjectNameInput.text;
                if (PV.IsMine)
                {
                    PV.RPC("SyncObjectName", RpcTarget.All, ObjectName.text, ObjectNameInput.text);
                }
            }
        }
        
    
        [PunRPC]
        void SyncObjectName(string Name, string NameInput)
        {
            Name = NameInput;
        }
    }