Clients not updating gameobject after RPC received.

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Clients not updating gameobject after RPC received.

Webbe
2021-08-02 13:47:31

Hello everyone!

So I'm currently dipping my toes into Photon and Multiplayer, one of the features that I thought could be a good learning experience would be to retrieve the Mesh and Material from a target and then change the client's Mesh and Material and then display that for all users. This would be similar to the Prop Hunt game mode I guess.

So I thought that I would give this a shot and I'm currently struggling to have the other clients see the change of Mesh and Material. It works fine in the editor and you can see on the clients that you've swapped Mesh and Material due to the shadow changing shape.

What I've done so far is to create a few prefabs that have a PhotonView on them, I'm trying to get the game object information from the PhotonViewId.

So here's my Mirror method:

    private void Mirror()  
    {

        if (!photonView.IsMine)  
        {  
            return;  
        }

        RaycastHit hit;

        if (Physics.Raycast(_camera.transform.position, _camera.transform.forward, out hit, _range))  
        {

            GameObject target = null;

            if (hit.transform.GetComponent<Mirrorable>())  
            {  
                target = hit.transform.gameObject;  
            }  
            

            if (target != null)  
            {  
                Debug.LogFormat("Sending RPC MirrorProp with the target {0}", target.GetPhotonView().ViewID);  
                photonView.RPC("MirrorPropRpc", RpcTarget.All, target.GetPhotonView().ViewID);  
            }        
        }       
    }

Then we have the actual RPC where I get the game object information and then I change my game object mesh and material if the checks are correct.

   [PunRPC]  
    private void MirrorPropRpc(int targetPhotonId)  
    {

        if (!photonView.IsMine)  
        {  
            return;  
        }

        PhotonView target = PhotonView.Find(targetPhotonId);

        if (target.gameObject == null)  
        {  
            return;  
        }

        Mesh targetMesh = target.gameObject.GetComponent<MeshFilter>().mesh;  
        Material targetMaterial = target.gameObject.GetComponent<MeshRenderer>().material;

        if (targetMesh != null && targetMaterial != null)  
        {  
            gameObject.GetComponentInChildren<MeshFilter>().mesh = targetMesh;  
            gameObject.GetComponentInChildren<MeshRenderer>().material = targetMaterial;

            Debug.Log("You have mirrored: " + target.transform.name);

        }  
        else  
        {  
            Debug.LogErrorFormat("PlayerMirror: MirrorPropRpc() - Unable to mirror prop, mesh = {0} and material = {1}", targetMesh, targetMaterial);  
        }    
    }

We can see from the Debug Log the following:

Sending RPC MirrorProp with the target 2

Sending RPC "MirrorPropRpc" to target: All or player:.

Received RPC: MirrorPropRpc

You have mirrored: Tree03_Yellow

I might just be misunderstanding the workflow of RPCs so any help would be really appreciated.

Comments

Webbe
2021-08-02 14:02:48

So I actually found the problem.. I guess you figure things out when you explain the problem for someone else huh.

The problem was the check if the photon view was mine in the RPC:

if (!photonView.IsMine)  
{  
    return;  
}

When I updated the method:

    [PunRPC]  
    private void MirrorPropRpc(int targetPhotonId)  
    {

        PhotonView target = PhotonView.Find(targetPhotonId);

        if (target.gameObject == null)  
        {  
            return;  
        }

        Mesh targetMesh = target.gameObject.GetComponent<MeshFilter>().mesh;  
        Material targetMaterial = target.gameObject.GetComponent<MeshRenderer>().material;

        if (targetMesh != null && targetMaterial != null)  
        {  
            gameObject.GetComponentInChildren<MeshFilter>().mesh = targetMesh;  
            gameObject.GetComponentInChildren<MeshRenderer>().material = targetMaterial;

            Debug.Log("You have mirrored: " + target.transform.name);

        }  
        else  
        {  
            Debug.LogErrorFormat("PlayerMirror: MirrorPropRpc() - Unable to mirror prop, mesh = {0} and material = {1}", targetMesh, targetMaterial);  
        }    
    }

Then everything works as expected! Thanks, everyone!

Tobias
2021-08-03 09:21:50

Glad you found the cause and could fix it! Thanks for the update, too.

Back to top