Delete game object

I have a character that I want it to pick rocks (once it picks the rock when A key is pressed> rock is destroyed) I can’t destroy on the tag because I am depending on the update if statement for it to work the way I want.

void Update()
{

    if (inTrigger && Input.GetKeyDown(KeyCode.A))
    {
        rockCount += 1;
        Debug.Log("Rocks Collected: " + rockCount);

    }

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.tag == "Rock") 
    {

        inTrigger = true;

    }

}

void OnTriggerExit2D(Collider2D other)
{
    if (other.gameObject.tag == "Rock")
    {

        inTrigger = false;
    }
}

You should introduce a List with references to the objects that you currently are in collision with. This will also fix the issue that your current code will not work if you collide with 2 rocks at the same time and pick up one.

So to solve this add a new List:

   private List<GameObject> rocksInCollision = new List<GameObject>();

then change your function:

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.tag == "Rock") 
     {
         if(!rocksInCollision.Contains(collision.gameObject))
                  rocksInCollision.Add(collision.gameObject);
     }
 }

then your OnTriggerExit function:

void OnTriggerExit2D(Collider2D other)
 {
     if (other.gameObject.tag == "Rock")
     {
         if(rocksInCollision.Contains(collision.gameObject))
                 rocksInCollision.Remove(collision.gameObject);
     }
 }

This way you can check in your Update function if the collisionList is empty. If it is not empty there is a rock to be picked up. At the same time you can destroy a rock from the List as you now have a reference to it.