Keep Raycast to Move Object Upright

I have a game where i move a cube with a ray cast that hits the cube and ‘picks’ it up(makes cube kinematic and parented to camera). However the cube has to stay upright. It will tip over if I look up to far. How can I make it so that the cube is always upright? Like when I pick it up it is upright constantly but when it is down it is affected by physic normally. I have tried locking rotation but that does nothing.

Code, if required can be given.

Thanks in advance.

stick this on your object to be picked up. this code should lock the rotation however it was picked up:

	bool b;
	Vector3 v;

	void Update(){
		if (transform.parent != null) {
				if (!b) {b = true;
					v = transform.eulerAngles;}

					transform.eulerAngles = v;}

		else{b=false;}
	}