Need help moving a cude backwards then forwards

Guys, I’m new to Unity and C#. I’m trying to make a cube move backwards until it reaches a certain point on the z position, then it then moves forwards. This is my code:

	void Update () {

		float zPosition = transform.position.z;
	
		if (zPosition >= -6.5) {
			transform.Translate (Vector3.back * Time.deltaTime);
		} else
			transform.Translate (Vector3.forward * Time.deltaTime);
		
	}

It works perfect until -6.5 where it stops and moves backwards and forwards as the zPosition is changing between >= -6.5 and <= -6.5. I just can’t think of another way to do this. As I mentioned, I’m new to Unity and C# so I’d appreciate any input.

Add in some extra state keeping variables outside the scope of that function.

bool moveBackFlag = false;
float minPos = -6.5f;
float maxPos = 6.5f;
void Update () 
{
    float zPosition = transform.position.z;
     
	if( moveBackFlag )
	{
		if( zPosition <= minPos )
			moveBackFlag = false;
	}
	else
	{
		if( zposition >= maxPos )
			moveBackFlag = true;
	}
    if (moveBackFlag)
        transform.Translate (Vector3.back * Time.deltaTime);
    else
        transform.Translate (Vector3.forward * Time.deltaTime);     
}