RPC calling GameObject

Options
  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.

Answers

  • rre
    Options

    Is difficult to understand where is the line error. Please explain better where is the line 33 . ;)

    In any case is more better ti send integer value than PhotonView reference: is an optimization.

    So try to change on this:

        [PunRPC]
        void RPC_Pickup(int _idItemView)
    {
    ...
                 if (isHolding) PV.RPC("RPC_Drop", RpcTarget.All, itemCurrentlyHolding.GetComponent<PhotonView>().ViewId, player.GetComponent<PhotonView>().ViewId);
    ...
    }
    
        [PunRPC]
        void RPC_Drop(int _ViewId, int _playerViewID)
    {
           GameObject _item = PhotonView.Find(_ViewID).gameObject;
    ...
    }
    
    

    and so on...

    Also is always better check If you really had found an object (you can also quick check with the debugger)

    For example

    if(itemCurrentlyHolding.GetComponent<PhotonView>()==null)
    {
    ... not found an obj
    }
    else
    {
    }
    

    hope help you

    R.

  • madrizivan
    Options

    Hi thanks for an answer, the error is in the Update and is when I called the RPC, I tried what you said but still have the error, I think that the error is happening because is a private game object that doesn't appear in the inspector