How can I make an object change direction on click?

This is my first post here, so if I did anything wrong, tell me!

Also, if there is an answer to this somewhere else, please link me to it. I have, personally, not been able to find anything.

I’m fairly new to programming, and unity, so any other tips on my code is welcomed!

I am trying to make it so when I press the mouse, my object (player) moves to the left, and when you press it again, it moves to the right. Each time enabling the other one so it will change the direction every time you click. My object will move to the right to start and move to the left when I click, but when I click again, it does nothing. If you’ve ever played Zig-Zag on mobile, that is kind of what I’m going for. (Except 2D, of course)

This is 2D and c#

using UnityEngine;
using System.Collections;

public class ShipController : MonoBehaviour {

public float Speed = .04f;
private bool goingRight;

// Use this for initialization
void Start () {

	goingRight = true;
}

// Update is called once per frame
void FixedUpdate () {

	if (goingRight == true) {
		transform.position += Vector3.right * Speed;
		goingRight = true;
	
	}  else {
		transform.position += Vector3.left * Speed;
	}

	if (Input.GetMouseButton (0) && goingRight == true) {
		transform.position += Vector3.left * Speed;
		goingRight = false;

	
	}

}

}

void FixedUpdate () {

     if (goingRight == true) {
         transform.position += Vector3.right * Speed;     
     }  else {
         transform.position += Vector3.left * Speed;
     }
 
     if (Input.GetMouseButton (0)) {
          goingRight  = !goingRight;
     }

your update function should be something like this.