Moving obstacle right and left between the walls, 3D

With this code my obstacle just start moving in one direction and pass through the wall, it doesn’t change direction. What to do?

int i = 1;
void Update()
{
    transform.Translate(0, 2 * i * Time.deltaTime, 0);
}
private void OnCollisionEnter(UnityEngine.Collision collision)
{
    if(collision.collider.tag == "Wall")
        i *= -1;
}

I move it on Y axis because obstacle is rotated.

Maybe it helps but not really sure cause I am not an expert. I just wanted to give a hint. The objects which are collided have box colliders? If yes and it still doesn’t work then try OnTriggerEnter and check IsTrigger.