localRotation.z returns wrong value

if(Physics.Raycast(mainRay, out hit, 4f))
{
if (Input.GetKeyDown(KeyCode.Mouse1) && hit.transform.tag == “cart”)
{
Debug.Log(hit.transform.eulerAngles.z);
}
}

This code is supposed to return the rotation (Z) of the certain object but instead of returning the value that is being shown at the inspector, it returns me a completely different value.

I tried to do it using hit.transform.localRotation.z too but it didn’t work either.

Your question title states something different from what you shown in your code.

localRotation is a Quaternion which is a 4 dimensional complex number. It’s components are always in range -1 to 1 and do not relate to euler angles. localEulerAngles are actual eulerAngles like those shown in the inspector. However Unity stores the rotation as Quaternion as Quaternions do not suffer from gimbal lock. Your rotation is actually in gimbal lock position as you rotated ±90° around x so eulerangles actually loose one degree of freedom. The Quaternion to eulerangles conversion is not unique. The same rotation can be expressed by several eulerangles combinations.

What is the actual problem you try to solve? For what purpose do you want to know the orientation of the object that your ray hits?