- Home /
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.
Answer by Joshua · Apr 28, 2011 at 05:40 PM
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
Thanks I'll have a look through this and get back to you. In the mean time any other answers are welcomed but I'll accept yours :).
Thanks. So you want to turn it into transform.Rotate(0,rotateSpeed * Time.deltaTime,0);
Just to add to this - what would be the best method if I did explicitly want left and right controls? I may have something in the game which inverts these for a period of time so it perhaps in my case would be suitable.
The documentation does not help here. It implies that you can set angles to absolute values using transform.localEulerAngles.x, etc, but that gives the same error as in the title of this post.
So, the question remains: How do you change just one of the rotation values?
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do I make a realistic jump? 1 Answer
Jet bike is not moving forward. 0 Answers
How do I make an object in game face the direction it's traveling while viewing from above? 1 Answer