How can I store each axis of a transform's rotation as a separate variable?

Quite simply, in the Start function I am trying to store the initial x, y, and z rotation values of a character into 3 separate float variables, NOT 1 Vector3 variable. I tried using transform.position.x or transform.eulerAngles.x to store the x rotation for example but I got the:

Cannot modify a value type return value of `UnityEngine.Transform…'. Consider storing the value in a temporary variable

So how could I do this? I can’t use a temporary variable because I will need to check these values in different sections of the code, maybe even across several components. I also need each axis stored separately because I will be making checks against each separately. Any suggestions for this small dilemma?

Your error does not match your description. Pulling variables out of rotation will not generate this error, but putting them back in will. For example in C# you cannot do:

transform.eulerAngles.x = 45;

Plus even if you could, the reference warns against assigning axes independently. It will cause strange rotations. You can do this:

public class Bug10 : MonoBehaviour {

	float x, y, z;
	
	void Start () {
		x = transform.eulerAngles.x;
		y = transform.eulerAngles.y;
		z = transform.eulerangles.z;
		
		Vector3 v3;
		v3.x = x;
		v3.y = y;
		v3.z = z;
		
		transform.eulerAngles = v3;
	}
}

Note the use of the temporary variable v3 when doing the assignment.

Are you using C#? Modifying “value type” elements from a property isn’t allowed in C#, but the friendly JS compiler does the job for us. In C# you must save the property to a temporary variable, modify the component in it and assign the variable back to the property. If you want to just modify X in transform.position, for instance, you must do this:

Vector3 tmp = transform.position; // read property in a temp variable...
tmp.x = 1.5f; // modify the desired component...
transform.position = tmp; // and assign the variable back to the property

But be careful when doing this with eulerAngles: you must save the initial value in a Vector3 member variable and never read eulerAngles again ! Do any change you want in the variable only and assign it to eulerAngles for the changes to take effect:

Vector3 euler0;

void Start(){
  euler0 = transform.eulerAngles; // read initial euler angles only once
}

When you want to set the X component, for instance, do it as follows:

  euler0.x = 90;
  transform.eulerAngles = euler0;

Never, jamais, under no circumstances modify individual components of eulerAngles - always assign the whole Vector3 to it, like above. eulerAngles is a completely virtual property: Unity internally reads transform.rotation, calculates and returns a combination of XYZ angles that corresponds to the rotation. Since there are several different combinations that result in the same rotation, Unity may (and probably will) return the wrong one.