C# language restrictions??

How can I bypass this error? Assets/Standard Assets/Runner1.cs(48,47): error CS1612: Cannot modify a value type return value of ‘UnityEngine.Transform.position’. Consider storing the value in a temporary variable. this is my script:

void Update () {
    if (distanceTraveled >= 13f){
        Camera.main.transform.position.x = 7.19f;
        Camera.main.transform.position.y = 0f;
        Camera.main.transform.position.z = 0f;
        Camera.main.transform.rotation.x = 0f;
        Camera.main.transform.rotation.y = 270f;
        Camera.main.transform.rotation.z = 0f;
    if (distanceTraveled < 13f){
        Camera.main.transform.position.x = 0f;
        Camera.main.transform.position.y = 2.74f;
        Camera.main.transform.position.z = -3.5f;
        Camera.main.transform.rotation.x = 12.57002f;
        Camera.main.transform.rotation.y = 0f;
        Camera.main.transform.rotation.z = 0f;

For the position :

Camera.main.transform.position = new Vector3(7.19f, 0f, 0f);

For the rotation :

Camera.main.transform.position = Quaternion.Euler(new Vector3(0f, 270f, 0f));

Just replace the values with your desired values.

More on this subject : here

This is not a language restriction but just a sideeffect of a valuetype like a Vector3. The problem comes from the fact that position is not a variable but a property. A property has a get and set method. When you access position you just call implicitly the get method and you get a copy of the Vector3, since it’s a value type. If you change the content of “x” you just change it in the copy. The compiler warns you about this fact and even suggests a solution.

Just copy it in a temporal variable, do your changes and assign it back:

Vector3 pos = Camera.main.transform.position;
pos.x = 7.19f;
pos.y = 0f;
pos.z = 0f;
Camera.main.transform.position = pos;

However since you don’t actually need any of it’s current values you can simply set the Vector directly:

Camera.main.transform.position = new Vector3(7.19f, 0f, 0f);

Next thing is .rotation is a quaternion. It’s 4 components doesn’t represent angles. You might want to use .eulerAngles instead.

ps: The UnityScript compiler actually adds some syntactic sugar and allows an assignment like you did by implicitly using a temp variable. However each assignment would use a seperate temp variable, so the compiler adds a lot of overhead. It’s not that bad but in most cases unnecessary.