'CS1612: Cannot modify the return value of 'Transform.position' because it is not a variable

Scripts:

using UnityEngine; using System.Collections;

public class camerafollow : MonoBehaviour {

public GameObject target;
public float scale = 4f;
public Vector3 maxPosition;
public Vector3 minPosition;

private Transform t;

void Awake()
{
    var cam = GetComponent<Camera>();
    cam.orthographicSize = (Screen.height / 12f) / scale;
}

// Use this for initialization
void Start()
{
    t = target.transform;
}

// Update is called once per frame
void Update()
{
    if (target != null)
    {
        Vector3 targetPosition = new Vector3(t.position.x, 
                                             t.position.y, 
                                             transform.position.z);
        t.position.x = Mathf.Clamp(t.position.x, minPosition.x, maxPosition.x);
        t.position.y = Mathf.Clamp(t.position.y, minPosition.y, maxPosition.y);
    
        transform.position = new Vector3(t.position.x, t.position.y, transform.position.z);
    }

}

}

ERROR: Assets\asm\scripts\Behaviors\camerafollow.cs(34,4): error CS1612: Cannot modify the return value of 'Transform.position' because it is not a variable Assets\asm\scripts\Behaviors\camerafollow.cs(35,4): error CS1612: Cannot modify the return value of 'Transform.position' because it is not a variable



Solution 1:[1]

I do not know an easy way either. I do like this:

void Update() {

//your code//


Vector3 temp = new Vector3(Mathf.Clamp(t.position.x, minPosition.x, maxPosition.x), 
                           Mathf.Clamp(t.position.y, minPosition.y, maxPosition.y),
                           t.position.z);
t.position = temp;
transform.position = new Vector3(t.position.x, t.position.y, transform.position.z);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Salih Tunçel