How to make a pick up script?

I saw a lots of people who like to Make a pick up script that have script to all the objects that he wants to pick but i tought it could be more optimised in a game where you have a lot of object to pickup if only the player have a script that shots a ray and take info of the object that the player want to pick up and parrent that object to the player ,i tryed to do something like that and all the things were good about picking up but when i wanted to let go of an object i could not and i assume it was a problet at unparenting but i don.t know because it was stuck on the player,could anyone help me to create a script like this because mine isn.t working,the code is:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.Input;

public class PickUp : MonoBehaviour
{
    public float pickUpDistance=100f; 
    private bool pick;
    private PlayerInputActions controls;

    private void Awake()
    {
        controls = new PlayerInputActions();
        controls.PlayerControls.PickUp.started   += _ => pick=true;
        controls.PlayerControls.PickUp.canceled  += _ => pick=false;
        
    }

    private void Update(){
        Debug.DrawRay(transform.position, transform.forward * pickUpDistance, Color.red);
        Ray ray = new Ray (transform.position, transform.forward * pickUpDistance);
        RaycastHit hit;
        bool result = Physics.Raycast(ray, out hit, pickUpDistance);
        
        if (result)
        {
            if (pick == true)
            {
                hit.transform.SetParent(transform);
            }
            else
            {
                hit.transform.SetParent(null);
            }
                

        }
    }
    private void OnEnable()
    {
        controls.Enable();
    }
    private void OnDisable()
    {
        controls.Disable();
    }

}

The script is attached to the camera

i tried to make it when the left mouse is pressed(PlayerControls.PickUp)to hold the object in front of the player,attached to the camera(like MySummerCar)and when i no longer press on the left button the object will fall(i will put a rigidbody on every object and disable gravity when is attached to the camera),if i would make it work i will put ability to throw when right button is pressed and something to be able to rotate the object when i press middle button for example but first i need to be able to unparrent it from the camera

Wow,i am impressed how many peoples are following this question and i am a little bit upset because there is no solution but i found one tutorial wich uses joints to attach the object to the player and i think i can finish this problem and let you guys with a script,i am not sure if it will work but i will try hard and post the script.
The tutorial also uses a separare script for every object.