RPC calling GameObjects

  1. Hi i am making a pickup script but i having errors implementing Photon, NullReferenceException: Object reference not set to an instance of an object PickUpController.Update () (at Assets/Scripts/PickUpController.cs:33)
    public Camera camera;
    public Transform player;
    float maxPickupDistance = 5;
    GameObject itemCurrentlyHolding;
    bool isHolding = false;


    float dropForwardForce = 9;
    float dropUpwardForce = 5;


    PhotonView PV;


    private void Awake()
    {
        PV = player.GetComponent<PhotonView>();
    }


    void Update()
    {
        if (!PV.IsMine)
            return;

        //Line error 
        if (Input.GetKeyDown("e"))
        {
            PV.RPC("RPC_Pickup", RpcTarget.All, itemCurrentlyHolding.GetComponent<PhotonView>());
        }

                //Line error 



        if (Input.GetKeyDown("q"))
        {
            PV.RPC("RPC_Drop", RpcTarget.All, itemCurrentlyHolding.GetComponent<PhotonView>(), player.GetComponent<PhotonView>());
        }
    }


    [PunRPC]
    void RPC_Pickup(PhotonView itemView)
    {
        GameObject _item = PhotonView.Find(itemView.ViewID).gameObject;


        Debug.Log("Pick");
        Debug.Log(itemCurrentlyHolding);
        RaycastHit hit;
        if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit, maxPickupDistance))
        {
            if (hit.transform.tag == "Item")
            {
                if (isHolding) PV.RPC("RPC_Drop", RpcTarget.All, itemCurrentlyHolding.GetComponent<PhotonView>(), player.GetComponent<PhotonView>());


                _item = hit.transform.gameObject;


                foreach (var c in hit.transform.GetComponentsInChildren<Collider>()) if (c != null) c.enabled = false;
                foreach (var r in hit.transform.GetComponentsInChildren<Rigidbody>()) if (r != null) r.isKinematic = true;


                _item.transform.parent = transform;
                _item.transform.localPosition = Vector3.zero;
                _item.transform.localEulerAngles = Vector3.zero;


                isHolding = true;


                
            }
        }


    }

    [PunRPC]
    void RPC_Drop(PhotonView _itemView, PhotonView _playerView)
    {
        GameObject _item = PhotonView.Find(_itemView.ViewID).gameObject;
        Transform _player = PhotonView.Find(_playerView.ViewID).transform;


        Debug.Log("Drop");
        _item.transform.parent = null;
        foreach (var c in _item.GetComponentsInChildren<Collider>()) if (c != null) c.enabled = true;
        foreach (var r in _item.GetComponentsInChildren<Rigidbody>()) if (r != null) r.isKinematic = false;
        isHolding = false;
        RaycastHit hitDown;
        Physics.Raycast(transform.position, -Vector3.up, out hitDown);




        var rb = _item.GetComponentInChildren<Rigidbody>();


        float random = Random.Range(-1f, 1f);
        rb.AddTorque(new Vector3(random, random, random) * 10);


        rb.AddForce(camera.transform.forward * dropForwardForce, ForceMode.Impulse);
        rb.AddForce(camera.transform.up * dropUpwardForce, ForceMode.Impulse);


        rb.velocity = _player.GetComponent<Rigidbody>().velocity;


    }

and i dont know what is happening because my object have the PhotonView implemented.

Comments

  • The art of debugging very often means simplifying the code so you can better target the reason for an error. If the error isn't occurring in the RPC you don't actually need to call them and can substitute a simple log statement so you can tell which condition is being met. Fewer lines of code makes it is easier to spot the problem.

    There are a couple of other things that look suspect to me. I for instance wouldn't typically pass a view as a parameter if you only require the ID. You are also (if you want to tidy up a bit) using parameters named _itemView and itemView. You name some local variables with underscores _item and _player (that is atypical).

    I'd choose a convention and stick with it throughout.