How Do I Tell if an Object Has an Object in Front of It?

In my game, I have blocks that are in a line, and when you press the left or right key, only the first object should move. This is basically what I’m trying to do:

If an object is not in front of this object {

If left arrow key is pressed {

Move to the left

} else {

Move to the right


If infront means along the checking objects local z axis:

if (objectA.transform.InverseTransformPoint(objectInfront.transform.position).z > 0)