Error modifying property in C#

Hi guys I’m converting a script from JavaScript to C# that has a snippet like this…

JavaScript Version

if ( transform.rotation.x > 0.05 )
	{
		transform.rotation.x = 0.05;
	}

If I just convert the same thing to C# I get this error
CS1612: Cannot modify a value type return value of `UnityEngine.Transform.rotation’. Consider storing the value in a temporary variable

Doing some research in the forum I’ve found that rotation.x is implemented as a property without a “set”, how can I achieve the same effect ? (Limiting the roation to some angle in the axis x)???

I’ve tried this but is not ok… :frowning: (the spaceship was shaking…)
transform.rotation = Quaternion.AngleAxis(0.05f,Vector3.left);

yeah, it’s annoying. here’s how to do it:

		Vector3 pos = transform.position;
		pos.x = 4;
		transform.position = pos;

rotations are quaternions though, so setting theit properties directly probably wont do what you think.

you may want to try

Vector3 rot = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(0.05,rot.x,rot.y);

though frankly I find using LookAt (aim) vectors easier than dealing with the actual rotation.

Thanks pakfrot, I’m at global gam jam (Brazil) and was stucked in this script problem. I had solved like this…

if (transform.rotation.z > 0.05f)
{
  //transform.rotation.z = 0.05;
  transform.rotation = new Quaternion(transform.rotation.x,transform.rotation.y,0.05f,transform.rotation.w);													
}
1 Like