pingpong ----- -

I have the following :

        float x = Mathf.PingPong(Time.time, 3);
    float y = gameObject.transform.position.y;
    float z = gameObject.transform.position.z;
    gameObject.transform.position = new Vector3(x, y, z);

It moves the gameObject back and forth However Mathf.PingPong(Time.time, 3) put the gameObject at some other place in the scene.

How can i fix that?

The trick is to specify a point that you want the object to pingpong around, something like this:

Vector3 centerPoint = new Vector3( 10, 10, 10 );

float xOffset = Mathf.PingPong(Time.time, 3);
gameObject.transform.position = centerPoint + new Vector3(xOffset, 0, 0);

Of course, then any script that is supposed to modify the objects position would need to modify `centerPoint` instead of `transform.position`.