Changing direction of a cube via transform.Translate vs. transform.position

I have a piece of script to make a cube go forward and after 100 steps change its direction to the opposite, and so on. I did this by using Translate method, and it works. Below is the script:

I’m a beginner, so I’m trying to understand differences between Translate and transform.position, so I basically want the cube to behave in the same way by using either Translate or transform.position. How should I do it? I tried to write script in the same way as via Translate - by using “counter” but it doesn’t work. The cube goes to one direction and never comes back. I tried several other ideas but they also didn’t work. Please help.

You are only reducing the value of zvalue by 0.1f once when the counter is 100 and then you keep adding 0.1f therefore moving it forever in the same direction. You are also forgetting to reset the counter. You need to add a direction variable just like you did in your first example.

float zvalue = 0;
int counter = 0;
int direction = 1;

void Update()
	Vector3 p = new Vector3(0, 0, zvalue);
	transform.position = p;
	zvalue += 0.1f * direction;
		direction *= -1;
		counter = 0;

Many thanks!!! It looks so easy but I couldn’t get there by myself