RPC call on another Gameobject
The whole answer can be found below.
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).
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
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)
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?
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.