Here's another way to think of it:
First, we figure out which direction in the parent space points toward world "up":
Vector3 parentSpaceUp = transform.parent.InverseTransformDirection(Vector3.up);
Then we orient ourselves to point forward in the parent's local coordinate, while aligning our own up vector as close as possible to this vertical:
transform.localRotation = Quaternion.LookRotation(Vector3.forward, parentSpaceUp);
Wherever possible, I like to use direction vectors and quaternions over Euler angles, both to avoid unnecessary trig functions, and also to make sure the code doesn't exhibit bugs at edge cases where the Euler angles wrap around.
C#
andUnityScript
. Are you sure you need an answer which explains how to do this in the old, deprecated UnityScript scripting language? If you only need a solution for writing Unity scripts in C#, please don't use that tag. \$\endgroup\$