[PUN 2] Change MeshFilter and MeshRenderer with RPC

Options
Hi! I'm new to Photon and a bit rusty with C#, I'm trying to create a simple Prop Hunt: players should be able to change their appearance by interacting with objects in the scene, changing their mesh and materials with the ones of the objects. Of course, I have to communicate this to other players.
I thought I could use an RPC like this:
//with a raycast i interacted with the right object and now i'm 
and now I'm collecting the info I need to change mesh and material
if (Physics.Raycast(ray, out RaycastHit hit))
        {
            GameObject tempHit = hit.collider.gameObject;
            if (tempHit.GetComponent<Prop>())
            {
               PV.RPC("RPC_PropChangeModel", RpcTarget.All, tempHit);
            }
        }
[...]
[PunRPC]
    void RPC_PropChangeModel(GameObject temp)
    {
        if (!PV.IsMine)
            return;

        gameObject.GetComponent<MeshFilter>().mesh = temp.GetComponent<MeshFilter>().mesh;
        gameObject.GetComponent<MeshRenderer>().material = temp.GetComponent<MeshRenderer>().material;
    }


This is not working, I keep getting this error message in console:
Exception: Write failed. Custom type not found: UnityEngine.GameObject
ExitGames.Client.Photon.Protocol18.WriteCustomType (ExitGames.Client.Photon.StreamBuffer stream, System.Object value, System.Boolean writeType)


How can I make this work? I can't find help online. Thx!

Comments

  • James_MF
    Options
    You're trying to send a GameObject through RPC. Instead, try getting the PhotonView.ViewID if it has a PhotonView and send that through the PV.RPC call instead of your "tempHit" object.
    PV.RPC("RPC_PropChangeModel", RpcTarget.All, tempHit.GetPhotonView().viewID);
    
    When that RPC gets called, simply:
    void RPC_PropChangeModel(int targetPropID)
        {
            if (!PV.IsMine)
                return;
    
            PhotonView targetPV = PhotonView.Find(targetPropID);
    
            if (targetPV.gameObject == null)
                return;
    
            gameObject.GetComponent<MeshFilter>().mesh = targetPV.gameObject.GetComponent<MeshFilter>().mesh;
            gameObject.GetComponent<MeshRenderer>().material = targetPV.gameObject.GetComponent<MeshRenderer>().material;
        }