Move Object A's Y position towards Object B's Y position

I’m attempting to make one object move towards another objects Y position.

I have 2 variables.

One called myTransform and one called playerTransform that I set in the start function

and I have this line in the update function.

myTransform.position.y = playerTransform.position.y;

I get an error when writing it. I’m guessing it isn’t possible to write it like this :confused:

Does anyone have a solution to this?

Thanks :slight_smile:

I am guessing you are using C#, because you can only write it like that in UnityScript.

You have to modify the entire Vector3 position instead, so try making a new Vector3 which does allow you to modify its values directly:

Vector3 newPosition = myTransform.position;
newPosition.y = playerTransform.position.y;
myTransform.position = newPosition;