Error writing to transform.position

Normally this is error is not a problem for me, but I can’t get it right this time. I’m trying to write to transform.rotation by storing the variables in a temporary variable then assigning the temp variable to the transform.rotation. Here is the error and the code. Please take a look and slap my hands if it was simple.

Assets/Scripts/Weapon.cs(24,35): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.rotation’. Consider storing the value in a temporary variable

			Vector3 currentAngles =	transform.rotation.eulerAngles;
			currentAngles.y = Mathf.LerpAngle(currentAngles.y,targetAngles.y,Time.deltaTime);
			transform.rotation.eulerAngles = currentAngles;

The error you’re seeing is fully explained and resolved in this MSDN article, much better than any information I could give you here:

Read that. :slight_smile: It will perfectly explain why this happens so you can avoid it in the future.

I can tell you though, as a hint, that it’s the third line you posted that causes the error, and it has to do with transform.rotation, not transform.position (maybe update your question title). It’s because you have to assign something directly to transform.rotation, not transform.rotation.eulerAngles. Consider using Quaternion.Euler to generate the required quaternion to assign to rotation.