How can i do to pickup and drop an object with the same key?

How can i do to pickup and drop an object with the same key?

I tried everything I saw online, but nothing works, it never drops the object, even with a different key, the only way i found to drop the object is to do “if G is pressed, pickup and else if G is pressed drop” so it drops as soon as the object is picked up and I can’t do better.
I hope someone has a solution for me

Here’s my code:

if(grabCheck.collider != null && grabCheck.collider.tag == "objets")
{
    if(Input.GetKey("g"))
    {
        if(!isGrabbing)
        {
            grabCheck.collider.gameObject.transform.parent= Holder;
            grabCheck.collider.gameObject.transform.position= Holder.position;
            grabCheck.collider.gameObject.GetComponent<Rigidbody2D>().isKinematic = true;
            isGrabbing = true;
            if(isGrabbing == true)
            {
                Debug.Log("isGrabbing");
            }
        }
        else
        {
            grabCheck.collider.gameObject.transform.parent = null;
            grabCheck.collider.gameObject.GetComponent<Rigidbody2D>().MovePosition(grabDetect.position);
            grabCheck.collider.gameObject.GetComponent<Rigidbody2D>().isKinematic = false;
            isGrabbing = false;
            if(isGrabbing == false)
            {
                Debug.Log("isn't grabbing");
            }
        }
    }
    
}

Your strategy of using isGrabbing to determine what to do in response to the key being pressed sounds correct to me. I’m not sure about your grabCheck collider check since I don’t know how your scene is set up.

However, you may want to use Input.GetKeyDown instead of Input.GetKey. GetKey will return true on every frame that the key is in the down position, so you are likely toggling between holding and dropping every frame. GetKeyDown is only true for a single frame when the key is pressed. It won’t trigger again until you release and press the button again.

Please note that GetKeyDown only works at Update time. You shouldn’t try to use this in an OnCollisionEnter function or anything like that.