Object Picking and Placing

I am trying to implement a game where the player can pickup some objects and place them somewhere else. I need the player to pick one object at a time. I found some code on youtube and it works perfectly fine sometimes but other times it gives problems such as :

Problem 1 : When the player picks the object and try to drop it at some other place , the object falls at the same spot from where it is picked even though it is moved after picking.

Problem 2 : Runtime error : Null Reference Exception at line 57.

The code i used :

[SerializeField] private LayerMask pickUpLayer;
[SerializeField] private Camera playerCamera;
[SerializeField] private Transform hand;
[SerializeField] private float pickUpRange;

private Rigidbody currentObjectRigidbody;
private Collider currentObjectCollider;

private Vector3 currentPosition;

private void Update()
{
    
    if (Input.GetKeyDown(KeyCode.P))
    {
        Ray pickUpRay = new Ray(playerCamera.transform.position, playerCamera.transform.forward);

        if (Physics.Raycast(pickUpRay, out RaycastHit hitInfo, pickUpRange, pickUpLayer))
        {
            if (currentObjectRigidbody)
            {
                //Exchange Objects

                /*currentObjectRigidbody.isKinematic = false;
                currentObjectCollider.enabled = true;

                currentObjectRigidbody = hitInfo.rigidbody;
                currentObjectCollider = hitInfo.collider;

                currentObjectRigidbody.isKinematic = true;
                currentObjectCollider.enabled = false;*/
            }
            else
            {
                //Picking


                currentObjectRigidbody = hitInfo.rigidbody;
                currentObjectCollider = hitInfo.collider;

                currentObjectRigidbody.isKinematic = true;
                currentObjectCollider.enabled = false;

            }
            return;
        }
        if (currentObjectRigidbody)
        {
            //Droping

            currentObjectRigidbody.isKinematic = false;
            currentObjectCollider.enabled = true;

            currentObjectRigidbody = hitInfo.rigidbody;
            currentObjectCollider = hitInfo.collider;

            currentObjectRigidbody.isKinematic = true;
            currentObjectCollider.enabled = false;
        }
    }

    if (currentObjectRigidbody)
    {
        currentObjectRigidbody.transform.position=hand.transform.position;
        //currentObjectRigidbody.rotation=hand.rotation;
        hand.transform.localScale=currentObjectRigidbody.transform.localScale;
    }

}

Can anyone help me?

Second problem is easy, the answer is ALWAYS the same:

How to fix a NullReferenceException error

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

http://plbm.com/?p=221

First problem just sounds like you wrote a bug… and that means… time to start debugging!

I would begin printing locations out, keeping your scene intentionally simple so you can reason if a location is correct by simply glancing at the printed numbers.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

Thanks, I will try it.

Didn’t you already make a thread about this?

Yeah, I did. The solution I got in that thread gave the same issue that this thread code gives. Then I changed the code but the problem still remains same. I just wants to know why is it working perfectly fine sometimes but not everytime.

Use MovePosition from FixedUpdate to move a kinematic rigidbody.

Line 57 of your script is assuming that currentObjectRigidbody has been set by line 54 but this won’t be true if the raycast didn’t hit a rigidbody. Also when dropping the object you should set currentObjectRigidbody to null.

Thanks, It worked.