Error says I don't have a PunRPC function that takes in int parameter, but I do

Options
This one's quite bizarre but I can't seem to figure it out. It looks pretty straightforward, here's my RPC function and the call -
(https://imgur.com/a/QDzdH0i)
public void OffensiveOccuppied(EPlayerController controller)
    {
        int tID = controller.GetComponent<PhotonView>().ViewID;
        Debug.Log("View Id = " + tID);

        PhotonView tView = GetComponentInParent<PhotonView>();

        tView.RPC("RPC_OffensiveOccupied", RpcTarget.All, tID);
    }

    [PunRPC]
    void RPC_OffensiveOccupied(int controller)
    {
        Debug.Log(name + " is now being controlled by " + controller);
        controllingPlayer = controller;
        isOccupied = true;
        PhotonView.Find(controller).transform.position = playerAnchor.transform.position;
        Camera cam = PhotonView.Find(controller).GetComponentInChildren<Camera>();
        cam.transform.SetParent(this.transform);
        cam.transform.localPosition = cameraAnchor.transform.localPosition;
        cam.transform.localRotation = cameraAnchor.transform.localRotation;
    }

So as can be seen here, I do have a PunRPC-marked function that takes an int parameter, but I still get this error message -

PhotonView with ID 1003 has no (non-static) method "RPC_OffensiveOccupied" marked with the [PunRPC](C#) or @PunRPC(JS) property! Args: Int32
UnityEngine.Debug:LogError(Object)

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @EurusGames,

    Thank you for choosing Photon!

    Your approach and code is a bit unusual.
    RPC methods work only on scripts that are attached to the same exact GameObject as the PhotonView where you call the RPC.

    So the "RPC_OffensiveOccupied" method needs to be implemented on a script attached to the same GameObject as the PhotonView that you use to call photonView.RPC("RPC_OffensiveOccupied"..

    Never do this by the way:
    PhotonView tView = new PhotonView();
    

    Since "OffensiveOccupied" and "RPC_OffensiveOccupied" are on the same script, the PhotonView needs to be attached to the same GameObject as that script.
    So no GetComponentInParent or GetComponentInChildren should not be used.

    Instead:
    PhotonView tView = GetComponent<PhotonView>();
    if (!tView)
    {
        Debug.LogError("PhotonView not found");
    }
    else if (tView.IsMine)
    {
    // [...]
    }