Search Unity

error CS1612

Discussion in 'Scripting' started by NintendoMaster00, Aug 30, 2015.

  1. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    This is my camera follow code for a 2D game. The problem I am having is that I am getting the error:

    error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

    It has to do with the Clamp camera but I am not sure how to fix this.

    Any help would be much appreciated.

    Code (CSharp):
    1.  
    2.     public Transform Target;
    3.     public float moveSpeed = 0.2f;
    4.     Camera mainCam;
    5.  
    6.     void Start ()
    7.     {
    8.         mainCam = GetComponent<Camera> ();
    9.         transform.position.x = Mathf.Clamp(transform.position.x,-10, 10); // to prevent the camera from going past a certain point
    10.         transform.position.y = Mathf.Clamp(transform.position.y,-10, 10); // to prevent the camera from going past a certain point
    11.     }
    12.  
    13.     void Update ()
    14.     {
    15.         mainCam.orthographicSize = (Screen.height / 100f) / 4f;
    16.  
    17.         if (transform)
    18.         {
    19.             transform.position = Vector3.Lerp(transform.position, Target.position, moveSpeed) + new Vector3(0, 0, -10);
    20.         }
    21.     }
    22. }
    23.  
     
  2. georetro

    georetro

    Joined:
    Jan 18, 2013
    Posts:
    218
    This is quite a common problem which I see a lot of people face. Basically you should correct it to

    Code (csharp):
    1. Vector3 temp = transform.position:
    2.  
    3. temp.x = Mathf.Clamp(transform.position.x,-10, 10);
    4. temp.y = Mathf.Clamp(transform.position.y,-10, 10);
    5.  
    6. transform.position = temp;
    This error happens because Vector 3 is a struct and transform.position is a getter method
     
    NintendoMaster00 and Kiwasi like this.
  3. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Thank you so much!!
     
  4. georetro

    georetro

    Joined:
    Jan 18, 2013
    Posts:
    218
  5. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Thanks again the error went away but the camera will still go past the points I specified

    here is my updated code if you know what I did wrong :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFollow : MonoBehaviour {
    5.  
    6.     public Transform Target;
    7.     public float moveSpeed = 0.2f;
    8.     Camera mainCam;
    9.  
    10.     void Start ()
    11.     {
    12.         mainCam = GetComponent<Camera> ();
    13.  
    14.         Vector3 vect = transform.position;
    15.  
    16.         vect.x = Mathf.Clamp(transform.position.x,-2f, 5f); // to prevent the camera from going past a certain point
    17.         vect.y = Mathf.Clamp(transform.position.y,-9f, 1f); // to prevent the camera from going past a certain point
    18.         transform.position = vect;
    19.     }
    20.  
    21.     void Update ()
    22.     {
    23.         mainCam.orthographicSize = (Screen.height / 100f) / 4f;
    24.  
    25.         if (transform)
    26.         {
    27.             transform.position = Vector3.Lerp(transform.position, Target.position, moveSpeed) + new Vector3(0, 0, -10);
    28.         }
    29.     }
    30. }
     
  6. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    You're clamping the values one time. Start() is called before the first frame an object is rendered on.

    Instead, move the clamping to the Update() function, probably after transform.position=Vector3 ect....
    Update is called every frame, before the object is rendered.

    So, you want.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFollow : MonoBehaviour {
    5.  
    6.     public Transform Target;
    7.     public float moveSpeed = 0.2f;
    8.     Camera mainCam;
    9.  
    10.     void Start ()
    11.     {
    12.         mainCam = GetComponent<Camera> ();
    13.  
    14.     }
    15.  
    16.     void Update ()
    17.     {
    18.         mainCam.orthographicSize = (Screen.height / 100f) / 4f;
    19.  
    20.         if (transform)
    21.         {
    22.             transform.position = Vector3.Lerp(transform.position, Target.position, moveSpeed*Time.deltaTime) + new Vector3(0, 0, -10);
    23.  
    24.             Vector3 vect = transform.position;
    25.  
    26.             vect.x = Mathf.Clamp(transform.position.x,-2f, 5f); // to prevent the camera from going past a certain point
    27.             vect.y = Mathf.Clamp(transform.position.y,-9f, 1f); // to prevent the camera from going past a certain point
    28.             transform.position = vect;
    29.         }
    30.     }
    31. }
    Also, I added *Time.deltaTime after moveSpeed. This means it will move at a constant rate no matter what your FPS is.
     
    NintendoMaster00 likes this.
  7. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86

    Thanks!