RPC call on another Gameobject

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.

RPC call on another Gameobject

Dufffit
2020-12-03 18:56:39

Hi,

unfortunately I am still struggling with the RPC calls.
In my game I have one Gameobject that should serve as a "button" (the type is not an UI button, its a 3D-Object with a Mesh) to (de-)activate another Gameobject. So if a click on the first Gameobject is recognized, the second one should be activated or deactivated for all clients.

I wanted to realize this via RPC calls. The script is attached to the "button"-Mesh, the GameObject and the PhotonView belong to the second GO which I want to (de-)activate.

If I test it in unity the console tells me:
"Exception: Write failed. Custom type not found: UnityEngine.GameObject"
but also:
"Sending RPC "SetThisInactive" to target: All or player:."

So is it not possible to synchronize the status of a Gameobject via RPC? How can I synchronize the (De-)Activation of an GameObject via PUN?

    public GameObject ToBeDeActivated;  
    public PhotonView PVToBeDeActivated;  
      
    private void OnMouseDown()  
    {  
        if (ToBeDeActivated.activeInHierarchy)  
        {  
            PVToBeDeActivated.RPC("SetThisInactive", RpcTarget.All, PVToBeDeActivated);  
        }  
        else  
        {  
            PVToBeDeActivated.RPC("SetThisActive", RpcTarget.All, PVToBeDeActivated);  
        }  
    }

    [PunRPC]  
    void SetThisActive(GameObject ToBeActivated)  
    {  
        ToBeActivated.SetActive(true);  
    }

    [PunRPC]  
    void SetThisInactive(GameObject ToBeDeactivated)  
    {  
        ToBeDeactivated.SetActive(false);  
    }  

Comments

Thato
2020-12-03 19:26:14

Hi,
You cannot send GameObject, PhotonViews, or complex types via the Photon Network. You can only send simple variable types such as strings, ints, and floats. Thus, your solution would be to use those instead. So locally you send the view ID of the game object you want activated/deactivated, and then on the remote side, you find that ID. If the sending and receiving GO is the same, this is unnecessary.
So...

private void OnMouseDown()  
{  
    if (ToBeDeActivated.activeInHierarchy)  
    {  
        PVToBeDeActivated.RPC("SetThisInactive", RpcTarget.All,  PVToBeDeActivated.ViewID);  
    }  
    else  
    {  
        PVToBeDeActivated.RPC("SetThisActive", RpcTarget.All, PVToBeDeActivated.ViewID);  
    }  
}

[PunRPC]  
void SetThisActive(int ID)  
{  
           PhotonView.Find(ID).gameObject.SetActive(true);       //WHEN IT IS NOT SAME OBJECT

    ToBeActivated.SetActive(true);  
}

[PunRPC]  
void SetThisInactive(int ID)  
{  

PhotonView.Find(ID).gameObject.SetActive(false); //WHEN IT IS NOT SAME OBJECT

    ToBeDeactivated.SetActive(false);  
}

But in your case, you can just call the RPC without having any parameters.

You're welcome B)

Dufffit
2020-12-05 23:49:22

Ah, nice, that makes sense! Thank you very much!

Unfortunately I still get an Error (No matter if I implement the functions with or without the PhotonView ID as a parameter):

"RPC method 'SetThisInactive(Int32)' not found on object with PhotonView 2. Implement as non-static. Apply [PunRPC]."

I mean, the function is not on the object with ID 2, the script is attached to the "Button" object. And [PunRPC] is applied...
So, I have to apply the functions marked with [PunRPC] on another script which is attached to the Object I want to deactivate/activate and call them on the script which is attached to the "Button" Object then?

Dufffit
2020-12-10 20:06:42

Update:
Now it works. As I already guessed, I had to create a second script and attach it to the object which should be (de)activated.
So the script where the RPC call takes place is attached to the Gameobject which serves as a button and the script where the [PunRPC] functions are defined is attached to the Gameobject which should be (de)activated.

Back to top