RPC call on another Gameobject

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

  • ThatoThato
    edited December 2020
    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?
  • DufffitDufffit
    edited December 2020
    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.
Sign In or Register to comment.