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

Options

I have a PUN RPC on a GameObject called Gun that has a script Gun on it, it is the child of the GameObject that has the PhotonView component attached.

I try to call the RPC from my PlayerController script (This script is attached to the parent object as well) but I get an error

RPC method 'Fire' found on object with PhotonView 1001 but has wrong parameters. Implement as 'Fire(Quaternion, Int32)

 [PunRPC]

  public void Fire(Quaternion Direction, int layer, Vector3 ThrowableLocation = default(Vector3))

  {

    if (PV.IsMine)

    {

      switch (GunType)

      {

        case GunTypes.Shotgun:

          float angle = -ShotgunSpreadAngle / 2;

          for (int i = 0; i < BulletCount; i++)

          {

            Vector3 bulletAngle = new Vector3(0, Direction.eulerAngles.y + angle, 0);//spread all bullets across angle

            angle += ShotgunSpreadAngle / BulletCount;

            GameObject go = Instantiate(BulletPrefab, transform.position, Quaternion.Euler(bulletAngle));

            go.layer = layer;


            //assign AI/Player script for updating ultimate charge

            if (transform.root.GetComponent<PlayerController>())

              go.GetComponent<BulletScript>().Player = transform.root.GetComponent<PlayerController>();


            go.SetActive(true);

            go.GetComponent<Rigidbody>().velocity = go.transform.forward * BulletVelocity;

            Destroy(go, BulletLife);

          }

          break;

        case GunTypes.Machine:

          float timer = 0;

          Vector3 BulletAngle = new Vector3(0, Direction.eulerAngles.y, 0);


          //fire machine bullets over time using coroutine

          for (int i = 0; i < BulletCount; i++)

          {

            StartCoroutine(ActivateBullet(timer, layer, Quaternion.Euler(BulletAngle)));

            timer += 1f / MachineFireRate;

          }

          break;

        case GunTypes.Throwable:

          GameObject projectile = Instantiate(BulletPrefab, transform.position, transform.rotation);

          Vector3 horizontalForce = ThrowableLocation - transform.position;

          projectile.layer = layer;


          //assign AI/Player script for updating ultimate charge

          if (transform.root.GetComponent<PlayerController>())

            projectile.GetComponent<BulletScript>().Player = transform.root.GetComponent<PlayerController>();

          else

            projectile.GetComponent<BulletScript>().AI = transform.root.GetComponent<AIController>();

          projectile.SetActive(true);

          projectile.GetComponent<Rigidbody>().AddForce(horizontalForce.x * 25, 500, horizontalForce.z * 25);//calculation for throwing grenade at designated position

          break;

      }

    }

And I call the method like this:

Gun.PV.RPC("Fire", RpcTarget.AllBuffered, transform.rotation, 8);

Answers

  • JohnTube
    JohnTube ✭✭✭✭✭
    Options

    Hi @MonsieurBond,

    Thank you for choosing Photon!

    As you can read on this documentation page:

    It is not recommended to use optional parameters in RPC methods. If necessary, pass all parameters including optional ones during the RPC call. Otherwise, the receiving clients will not be able to find and process the incoming RPC.

  • MonsieurBond
    edited November 2021
    Options

    Hi John,

    Thank you for answering.

    I removed the optional parameter, but I still get the error:

    RPC method 'Fire(Quaternion, Int32, Vector3)' not found on object with PhotonView 1001. Implement as non-static. Apply [PunRPC]. Components on children are not found.


    Do I need to attach a PhotonView to the Gun GameObject?