Cannot modify a value type return value of `UnityEngine.Transform.localEulerAngles'.

Hi guys, I'm pretty new to Unity and C#. I am just making a basic movement script and I have forward and backwards working as I would like them, however with left and right I have been using the following code:

if (Input.GetButton("Left"))
        {
            transform.localEulerAngles.y += rotateSpeed * Time.deltaTime;
        }

Which gives me the following error:

`Cannot modify a value type return value of`UnityEngine.Transform.localEulerAngles'`.

I have seen something about this before and it suggested adding the below code, I added it just below update() but I still get the same error:

transform.localEulerAngles = new Vector3(
            10f,
            transform.localEulerAngles.y,
            transform.localEulerAngles.z
);

Any help and an explanation would be much appreciated.

Hey, welcome to Unity and welcome the Answers.Unity3D.com.

When using pieces of code you're not entirely familiar with, also look them up in the docs

In this case it actually warns for the problem you are experiencing:

"Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees. Use Transform.Rotate instead."

Good luck!


edit: it's also convienent to use Input.GetAxis instead of directly using a left/right key. The advantage is that they are easy to set up, can be used throughout your entire project and will actually increment. When you first press left it'll be 0.01 and go up to 1 in about .5 seconds and stays there. This smooths everything out. Check 'm out here: Input