Raycast Not Working properly

I’m using Unity 2D. Ok so I only want the raycast to go up from the game object, which I think it does but I only want it go to up in one direction however it seems like it goes out from the object in every direction, I did test and my character was far away from it and the object got destroyed which is only supposed to happen when you jump “over” it IE: hitting its raycast. The game object also gets destroyed from beneath which isn’t supposed it happen and it gets destroyed from various angles which also isn’t supposed to happen. And it also gets destroyed from any side no matter the distance it seems like it.

So to summarize what I want to happen: Only have the raycast go upwards vertically from the object (not in any other direction) and it only gets destroyed when hitting that raycast. I tried to change the numbers and even change vector 3 to vector 2 and vice-versa and I get the same result from the first paragraph. enter code here

 RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector3.up, 5);
        if (hitInfo.collider.gameObject.tag == "Enemy")
        {
            Debug.DrawLine(transform.position, hitInfo.point, Color.white);
           
        }
        else
        {
            Debug.DrawLine(transform.position, Vector3.up * 5, Color.white);
 
            Destroy(GameObject.FindWithTag("Enemy"));
         
        }
    }

Try this

void CheckObjectPositition (Vector2 direction, float distance)
{
RaycastHit2D Ray = Physics2D.Raycast (transform.position, direction, distance, 1 << LayerMask.NameToLayer (“object”));

   if(ray.collider != null)
   {
          Destroy (gameObject);
    }

}

The above code should work provided you give the parameters their appropriate values.
For direction it is the direction for the day and for distance is the distance of the day
The LayerMask check will only work if you assign the object a layer of name object.

If you don’t understand anything notify me