This piece of Javascript is giving me troubles.

var originalPosition : Vector3;
var osHeight = 0.05;
var delay = 0;

function Start () {
    // when the object starts, we record its initial position
    originalPosition = transform.position;
}

function Update () {
    // Set the x position to loop between values osHeight and a set delay.
    transform.position.y = originalPosition.y + Mathf.Sin(Time.time + delay) * osHeight;
    Debug.Log("YPosition = " + transform.position.y);
}

All seems fine but only runs on play when the object is selected like this:

Otherwise it doesn’t run.

I’ve tried the debugger and I’ve noticed that the y position does oscilate. However the value on the object and of YPosition is different.

Why is this happening and how do I fix this?

Sorry if it’s a really simple question. I’ve tried googling answers but can’t seem to fix it.

you can’t change transform.position.y directly. you need to save the current position as Vector3, alter y and write it back to position.