Help With Basic AI, Keep Specific Distance From Player

I have just begun an independent study on Unity at my University, so I am very new to this. I have successfully created an AI which will more towards the player, collide with the player, and do damage to the player. I am now trying to modify this script in order to create a new enemy type which will move up to the player, but then maintain a constant distance from the front of the player and fire projectiles.

Here is what I have tried:

using UnityEngine;
using System.Collections;

using UnityEngine;
using System.Collections;

public class yellowCubeAI : MonoBehaviour {
	public Transform target;
	public int moveSpeed;
	public int rotationSpeed;
	private float distance;
	private Transform myTransform;
	void Awake(){
		//caches transform
		myTransform = transform;

	// Use this for initialization
	void Start () {
		GameObject go = GameObject.FindGameObjectWithTag("Player");
		//set target to player
		target = go.transform;
		//calculate distance
		distance = Vector3.Distance(target.position, transform.position);
	// Update is called once per frame
	void Update () {
		Debug.DrawLine(target.position, myTransform.position);
		//look at player
		myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
		//move to target
			myTransform.position +=myTransform.forward * moveSpeed * Time.deltaTime;
		else if(distance<=19){
			myTransform.position +=myTransform.forward * (-1) * moveSpeed * Time.deltaTime;

As of now this AI behaves just like my other, and simply runs into the player. Any help is appreciated!

you are only setting the distance in the Start function.
You need to continously update the distance in the Update function.

distance = Vector3.Distance(target.position, transform.position);

Hm, even when moving the distance calculation into the update function, this cube still behaves just like my other cube and simply runs into and follows the player.