All items in the whole scene disappear once a single one is picked up

I’m making a game about building engines and surviving.

I have this Consumable script but when i mean to consume one thing it consumes all of them and I don’t know how to fix this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Consumable : MonoBehaviour
{
    [SerializeField] private Camera cam;
    [SerializeField] private float range;

    [SerializeField] private GameObject Cursor;
    [SerializeField] private GameObject Hand;

    [SerializeField] private int RemoveThirst;
    [SerializeField] private int RemoveHunger;

    [SerializeField] private int AddThirst;
    [SerializeField] private int AddHunger;
    
    void Update()
    {
        RaycastHit hit;
        if(Input.GetKeyDown(KeyCode.F) && Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range) && hit.transform.gameObject.tag == "ConsumableItem")
        {
            Hand.SetActive(false);
            Cursor.SetActive(true);
            ThirstCountScript1.thirst -= RemoveThirst;
            ThirstCountScript1.hunger -= RemoveHunger;
            ThirstCountScript1.thirst += AddThirst;
            ThirstCountScript1.hunger += AddHunger;
            Destroy(gameObject);
        }
        if(Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range ) && hit.transform.gameObject.tag == "ConsumableItem")
        {
            Hand.SetActive(true);
            Cursor.SetActive(false);
        }
        else
        {
            Hand.SetActive(false);
            Cursor.SetActive(true);
        }
    }
}

You definitely need to separate two concerns here:

  • looking for consumables
  • being consumable

because right now these are the same component and this is not a recipe for success.

Consumable.cs

using UnityEngine;

//[RequireComponent( typeof(Collider) )]// requires a collider of some sorts to block raycasts
public class Consumable : MonoBehaviour
{
    public int RemoveThirst;
    public int RemoveHunger;
    public int AddThirst;
    public int AddHunger;
}

Consumer.cs

using UnityEngine;

public class Consumer : MonoBehaviour
{

    [SerializeField] Transform _cameraTransform;
    [SerializeField] float _maxRaycastRange = 10;
    [SerializeField] GameObject _cursor;
    [SerializeField] GameObject _hand;
    [SerializeField] LayerMask _layerMask = ~0;

    void Update ()
    {
        if( Physics.Raycast(_cameraTransform.position,_cameraTransform.forward,out RaycastHit hit,_maxRaycastRange,_layerMask) )
        {
            var consumable = hit.collider.GetComponent<Consumable>();
            if( consumable!=null )// this is consumable
            {
                // show hand
                _hand.SetActive(true);
                _cursor.SetActive(false);

                // check for key down
                if( Input.GetKeyDown(KeyCode.F) )
                {
                    // consume
                    ThirstCountScript1.thirst -= consumable.RemoveThirst;
                    ThirstCountScript1.hunger -= consumable.RemoveHunger;
                    ThirstCountScript1.thirst += consumable.AddThirst;
                    ThirstCountScript1.hunger += consumable.AddHunger;
                    Destroy( consumable.gameObject );
                }
            }
            else// this is some other object
            {
                // show cursor
                _hand.SetActive(false);
                _cursor.SetActive(true);
            }
        }
    }
}

Thank you so much i have been wondering why does this thing keep happening but seriously Thank you