How to get object's X axis rotation EXACTLY as in inspector ?

I just want to know How to get object’s X axis rotation EXACTLY as in inspector ?
I tried transform.eulerAngles.x it works at the beginning but when the X axis in the inspector hits 83 the one in the script starts to decrease on its own, and when the one in the script hits 0 it goes up to 340 directly…
and I also tried transform.rotation.x and when the x axis in inspector hits like 10 the one in the script is something like 0.9465872 O_o

So how to get the EXACT same value in the inspector’s Transform window ?


The short answer is: You can’t get the exact value as the editor will actually cache the value internally for editing. The problem is that Unity itself (the engine) does not work with euler angles at runtime but with quaterions. When you read transform.eulerAngles it will actually convert the quaterion to eulerAngles. Since eulerAngles are not a unique representation of an orientation there are several ways how the same orientation could be represented. Just as an example the eulerangles (0,0,0) is the same as (180,180,180) or (180, -180, 180).

The editor tracks a seperate angle value for editing in the inspector. It’s used when you actually edit the rotation in the editor. This value does not wrap around but simply continues to increase / decrease.

If you want the eulerangles to be in a certain range, you need to adjust them yourself. For example if “y” should never be negative, just add 360 if the value that transform.eulerAngles.y returns is negative. X usually has the range of ± 90°. Anything larger could be represented by an angle smaller than 90 and y and z rotated by 180°. So the angle (100,30,0) would be the same as (80,210,180).

As i said there simply is no “correct” or unique way how the eulerangles should represent a certain rotation. Since you haven’t mentioned your actual problem you want to solve we can’t suggest a different approach. You shouldn’t rely on the eulerangles conversion in the first place.

If you have never heard about Quaternions and you want to know how they work, have a look at this Numberphile video

Ok, Here is the problem:
I made a day/night cycle script (just directional light rotating in X axis in a variable speed) I also have some low poly game objects (clouds) And I want the opacity of their materials (Aplha channel) to be equal to the sun movement So that if its day time the clouds would be visible and its fading out, and when its night time they are invisible and fading in…
All of this are working in my script but the timing of fade in/fade out are wrong… I tried to debug.log the rotation of x axis (which I didnt know its impossible…) and it came wrong… Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DayNight : MonoBehaviour {
    public GameObject[] clouds;
    public float speed = 1.0f;
    public float sunPos;

    void Update() {
        transform.Rotate(speed * Time.deltaTime, 0, 0);
        sunPos = transform.eulerAngles.x;                // here is the problem
        clouds = GameObject.FindGameObjectsWithTag("Clouds");
        foreach (GameObject item in clouds) {
                Color color = item.GetComponent<Renderer>().material.color;
                color.a = 1 - sunPos;
                item.GetComponent<Renderer>().material.color = color;