How can a Ray detect more than one object in it's casted direction?

I have two objects located in a ray’s direction, one behind the other, as in the picture

I’m looking for a way to detect them both using ray, but so far only the object A, the one in front, is being detected. how can I return both as Raycast hits?

Use Physics.RaycastAll(). But then you have to go through and filter to process only the hits you want.