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