How would I move an Object after collision?

I have my game set up in 2D mode, There is a side scrolling camera with a bar that follows it, during the game bricks will move across the screen, when they disappear off screen I am trying to get them to collide with the bar following and the move back by an x value of Random.Range(-6,-8) I am unaware if my code is wrong, or if I have an error, but I will post what code I have used.

    function OnCollisionEnter(collision:Collision){
    collision.transform.position = Vector3(Random.Range(-6,-8), Random.Range(-3,6),0);  

If your bar collider is moving right then Just Make Sure that

  1. Your bricks/Bar has RigidBody. Because Collision is detected by Physics. So you will have to make one of them rigidbody.
  2. If you have Followed Step 1 and still your code is not working then you have problem in bar position. Just make the Bar child of camera.

I hope this should resolve your problem.