# Cannot modify a value type return value of UnityEngine.Transform.position'. Consider storing the value in a temporary variable

I'm getting the following error

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

with this code:

transform.position = new Vector3(target.position.x, transform.position.y, target.position.z);
Vector3 desiredPosition = new Vector3(transform.position.x , target.position.y, transform.position.y );
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position += smoothedPosition;

• This needs substantial improvement as a question. Dec 9 '17 at 17:28
• I want the camera to have Lerp on the y axis but not on the x and z. Dec 9 '17 at 17:31
• The code so far. transform.position = new Vector3(target.position.x, transform.position.y, target.position.z); //gameObject.transform.position.x = target.transform.position.x; //Vector3 actualPositionY = transform.position.y; Vector3 desiredPosition = new Vector3(transform.position.x , target.position.y, transform.position.y ); Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); transform.position += smoothedPosition; } Dec 9 '17 at 17:31

If you want to modify the transform.position property, you have to modify the transform.position property: it's values are effectively read-only. This is due to the fact that Unity Vectors themselves are value types and not instanced objects. What you're trying to do is similar to doing 4 += 1 which makes no sense.

Vector3 pos = transform.position;
pos.x = target.transform.position.x;
transform.position = pos;

• This is a bit offtopic...but how do I implement this to make so that I have smooth view on y but not on x and z.. Dec 9 '17 at 18:17
• Vector3 pos = transform.position; pos.x = target.position.x; pos.y = target.position.y; pos.z = target.position.z + offsetZ; Vector3 desiredPosition = new Vector3(pos.x + offsetX, target.position.y, pos.z + offsetZ ); Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); transform.position = desiredPosition; Dec 9 '17 at 18:18
• You don't need desiredPosition there at all, that's what pos is doing. With your desiredPosition value, you've added offsetZ twice (you already added it to pos.z on the previous line). Then, you create a lerp value (smoothedPosition) and then don't do anything with it. Dec 9 '17 at 18:28
• New code: Vector3 pos = transform.position; pos.x = target.position.x; pos.y = target.position.y; pos.z = target.position.z + offsetZ; Vector3 smoothedPosition = Vector3.Lerp(transform.position, pos, smoothSpeed); transform.position = smoothedPosition; Dec 9 '17 at 18:38
• I dont want the x,z to have lerp, just the y...how do i do that? Dec 9 '17 at 18:39

You can only store whole vectors to gameObject.transform.position

Position is read-only in all other contexts.

• I am new to coding...can you explain more...or what I need to change.Thanks Dec 9 '17 at 17:32
• Yeah, let me get to a computer and I'll suggest an edit to the question, and go into more detail. Dec 9 '17 at 17:33
• Draco beat me to the full explanation, but yes that's what I meant be read-only. You can only set values to it, not updated values in it. Dec 9 '17 at 18:42
• :) I dont want the x,z to have lerp, just the y...how do i do that? Dec 9 '17 at 18:44