Making an object move up on click, then move back down when clicked again.

Basically I am trying to make a hexagon move up when clicked, and if I click it again, make it move down, however when I click the hexagon, it just stays in the same position.

public class Hex_Movement : MonoBehaviour {
	//public float speed = 0.1f;
	public float clicked = 0;

	void Start () {
		

	}
	

	void Update () {
		
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit hitInfo;
		if (Physics.Raycast (ray, out hitInfo)) {

			GameObject hitobject = hitInfo.collider.transform.gameObject;
			if (Input.GetMouseButtonDown (0)) {

				if (clicked == 0){
					hitobject.transform.Translate(Vector3.forward);
					clicked += 1;
				}
				if (clicked == 1) {
					hitobject.transform.Translate (Vector3.back);
					clicked -= 1;
				}

			
					
			}

	}
}
}

But if I remove
if (clicked == 1) { hitobject.transform.Translate (Vector3.back); clicked -= 1; }

It will move up… any help is appreciated.

You’re raycasting every frame, which can decrease performance. You should instead call the Physics.Raycast function if the mouse button was pressed. In addition there’s some optimization that can be done. I’m sure there’s even more than can be done, but this code works:

public int clicked = 0;

	void Update () {
         if(Input.GetMouseButtonDown(0)){
         	Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         	RaycastHit hitInfo;
	        if (Physics.Raycast (ray, out hitInfo)) {
	        	GameObject hitobject = hitInfo.collider.transform.gameObject;
	        	//Keeping this ^^^ because I'm assuming you'll do more with the object than move it. Otherwise, there's no reason to store it as a variable.
	            hitobject.transform.Translate(Vector3.forward * ((clicked%2==0)?1:-1));
	            clicked++;
	        }
        }
    }