My code deletes all items. How do i make it only delete one?

I’m making a game and when i wrote my code to pickup items.

I kept running into the problem it would delete all items that had the code to be able to be picked up.

How do i make it only delete one?

var hasScript = gameObject.GetComponent<RemovableObject>();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
    var selectionTransform = hit.transform;
    if ( Vector3.Distance(selectionTransform.position, GameObject.FindGameObjectsWithTag("Player")[0].transform.position) < SelectionManager.Instance.interaction_distance && selectionTransform.GetComponent<RemovableObject>())
    {
        PlayerInRange = true;
    }
    if (Input.GetKeyDown(KeyCode.Mouse0) && PlayerInRange == true)
    {
        Destroy(gameObject);
        Debug.Log("item moved to inventory");
    }
}

The script you posted should be added to only ONE gameobject - that gameobject could be the player object, for example. If you add the script to multiple gameobjects then the code will be executed for every one of those gameobjects.

The reason why they are all destroyed is because Destroy(gameObject) means destroy the object that owns the component that invoked the Destroy() method.

So, a quick fix to your problem could be this:

  1. First, place the code you posted in a component called something along the lines of PickupController.
    Add this component to your Player (or any other unique, manager-like gameobject).
    I cannot stress this enough: Only ONE gameobject in the whole hierarchy is allowed to have the PickupController component.

  2. In the newly created PickupController component,
    replace:
    Destroy(gameObject)
    with:
    Destroy(selectionTransform.gameObject)

  3. Keep the RemovableObject component on all the removable gameobjects, but remove the code you posted from the RemovableObject script.
    The RemovableObject component in this case will be used solely to determine if the Raycast hit an object that you consider removable. This component doesn’t need to contain any logic at all.

EDIT:
I forgot to ask what’s surrounding the code you posted? Any input-related if-statements? Which Unity method is it in? Update?
I’m asking because Raycasts should be used sparingly as they can be quite expensive.
Your Input.GetKeyDown() check happens AFTER the Raycast. Is that intentional?

You should also try googling: Performance GameObject.FindGameObjectsWithTag
Because FindGameObjectsWithTag() is also a very expensive method if invoked many times per frame.