2D means that only X an Y axis matters but when i do
Objects with different Z value will not collide, as Collider2D is also 2D which lies on XY plane and the Z is same as the objects Z.
So, if you change Z of a 2D object they wont collide. Hopefully the attached image clarifies.
For different Z valued objects to collide, you'll need to use 3d colliders.