How to set y rotation of an object to be z rotation of another?

I figues that this would be really straightforward but it seems not.

I have two objects

CubeObject (Just a cube in my scene)
Camera (My main camera)

I want CubeObjects Y rotation to be the Z rotation of the Camera

this is a script which I place on my CubeObject

transform.rotation = new Quaternion(Camera.main.transform.rotation.x, Camera.main.transform.rotation.y, Camera.main.transform.rotation.y, Camera.main.transform.rotation.w);

It seems as if will work while i rotate the camera through its y axis but then everything goes crazy and the cube turn in a weird direction

Try this:

var cube : Transform;
var cam : Transform;

function Update () {
	cube.eulerAngles.y = cam.eulerAngles.z;
}