Get old parent and put object back

We pick up our item using this in void stay which works fine but keeps the worlds/players rotation of the item being picked up so it is at a different rotation depending on the position I am standing at when the button is pushed.

            if (Input.GetKeyDown(KeyCode.R) && other.gameObject.CompareTag("HandHeld") && leftFree == true)
            {
                leftFree = false;  //make sure we dont pick up another
                lastTime = Time.time; //add delay to key press so we don't drop item
                leftChild = other.gameObject; //get the original parent
                other.gameObject.transform.parent = lefthand.transform; //attatch the item
            }

Then I try to drop with this which does not get the job done at all, I presume I am just changing it to the current parent instead of the old one?

            if (Input.GetKeyDown(KeyCode.R) && (Time.time - lastTime > 1.0f) && leftFree == false)
            {
                leftChild.transform.parent = oldParent.transform.parent;
            }

Half tempted to just destroy the object and instantiate a new one at the player position but it would be nice if I didn’t need to and could just drop the current object instead.

Any ideas?

Problem 1: Save the original rotation/position before picking the object up. Restore rotation/position on drop.

Problem 2: Where are you setting oldParent? Shouldn’t you be setting it to other.transform.parent in the first snippet?

Sorry should of left it in, I had removed it as I was getting an error when interacting with the object when I was trying to figure it out.

I had set it just after I set leftChild
oldParent.transform.parent = other.transform.parent;

which gives me
NullReferenceException: Object reference not set to an instance of an object

I ended up with replacing the above line with

oldParent = other.gameObject;

and this to drop

            if (Input.GetKeyDown(KeyCode.R) && (Time.time - lastTime > 1.0f) && leftFree == false)
            {
                Debug.Log("drop the case");
                leftChild.transform.parent = oldParent.transform.parent;
                //Destroy(leftChild);
            }

It doesn’t kick up any errors but its not dropping the item either :frowning:

I fear I am just referencing the new location now instead of the previous.

You’re confusing yourself. Shooting blindly in the dark will not lead you to working code.

You’re setting oldParent to other, and then setting other’s transform parent to lefthand. Hopefully you can see why setting leftChild’s (set to other) parent to oldParent’s (which is also set to other) parent is just setting other’s parent to other’s parent.

Reread my original post; I didn’t tell you to set oldParent.transform.parent to other.transform.parent, or to set oldParent to other.gameObject. I told you to set oldParent to other.transform.parent. Take a moment and contemplate what the difference between these things are.

If it helps, translate your logic into sentences:

What you’re trying to do is save the object’s original parent, and restore it later.

oldParent.transform.parent = other.transform.parent; becomes “Set oldParent’s parent to other’s parent”. That’s not what you want! That’s not saving anything.

oldParent = other.gameObject; becomes “Save oldParent as other’s gameObject”. That’s not it either. You’re saving the object itself, rather than its parent.

oldParent = other.transform.parent; becomes “Save oldParent as the object’s original parent”. This is what you want.

1 Like

You sir are a scholar and a gentleman, thank you!

One very weird problem(for me anyway) I can pick the object up twice but on the third time it will pick the object up just fine but when I try to drop the item for the third time I get a

NullReferenceException: Object reference not set to an instance of an object

        private void Update()
        {
            if (!m_Jump)
            {
                m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
            }

            if (Input.GetKeyDown(KeyCode.R) && (Time.time - lastTime > 1.0f) && leftFree == false)
            {
                leftChild.transform.parent = oldParent.transform.parent;
                leftFree = true;
            }
        }
        private void OnTriggerStay(Collider other)
        {
            //Statement for handling contact with vehicles
            if (Input.GetKeyDown(KeyCode.F) && other.gameObject.CompareTag("Car"))
            {
                Destroy(gameObject);
                other.GetComponent<UserControl>().enabled = true;  //enables the controls of selected vehicle
                other.GetComponent<UserControl>().lastTime = Time.time;  //stops us pushing f when we enter the vehicle
                other.GetComponent<CarSoundController>().enabled = true;
                other.GetComponent<AudioListener>().enabled = true;  // move our ears to the new object
                playerCamera.GetComponent<CameraController>().player = other.gameObject;  //move our eyes to the new object
                uiElement.SetActive(false);  //clears the previous UI
            }

            if (Input.GetKeyDown(KeyCode.R) && other.gameObject.CompareTag("HandHeld") && leftFree == true)
            {
                leftFree = false;  //make sure we dont pick up another
                lastTime = Time.time; //add delay to key press so we don't drop item
                leftChild = other.gameObject; //get the original parent
                oldParent = other.transform.parent; //store the original parent
                other.gameObject.transform.parent = lefthand.transform; //attatch the item
            }
        }

I figure it has somehow lost the child(leftChild) but how/why? also each time it is dropped it moves up one level from its parent.

so it starts off in interactable-> handheld → Our Item

the first time its dropped it moves to “interactable” then on the second drop its ends up in the root so when it comes to the third time to drop the item it has no where it can go

Reread your advice, sorted, thank you once again I was going for its parent instead.

Can you pick answers on the forum btw :stuck_out_tongue: