- Home /
transform.position error
hey, got an error, the code is correct, i dont understand how to solve the error :/ http://pastie.org/1009827
if (Input.GetAxis("Mouse ScrollWheel") < 0) { Camera.main.transform.position.y = Camera.main.transform.position.y + 0.1; } if (Input.GetAxis("Mouse ScrollWheel") > 0) { Camera.main.transform.position.y = Camera.main.transform.position.y - 0.1; }
line 2 and line 4 = Cannot modify the return value of `UnityEngine.Transform.position' because it is not a variable
for ppl who are interested for the full code then : http://pastie.org/1009839
p.s. stating "the code is correct" generally doesn't help, when you are asking for assistance with some code which has errors!
Answer by duck · Jun 18, 2010 at 10:11 AM
This is because you're coding in C#. In javascript the compiler lets you modify the conmponent values of transform.position, but in C# it doesn't - you have to make a whole new Vector3 and assign it in one operation.
In Javascript, the compiler basically 'hides' the fact that it is doing this for you behind the scenes.
So, what you need to do is create a new Vector3 and assign it in one go, like this:
Vector3 newPosition = Camera.main.transform.position.y;
newPosition.y += 0.1f;
Camera.main.transform.position = newPosition;
Also, for simply moving a transform's position, you can use this:
Camera.main.transform.Translate( 0, 0.1f, 0);
Also you'll need to put 'f's after every floating point number (like I just did with the 0.1).
Your answer
Follow this Question
Related Questions
Place a object in front the camera in the coordinate 0 of Y axis 2 Answers
Change object position on trigger enter 3 Answers
Prevent changing position 1 Answer
How to make a sine wave with a transform 2 Answers