Basic Enemy AI in C#

Hello, I’m new to scripting in Unity and have been trying to script a basic enemy AI for my project. The idea is there is a trigger that the EnemyTerritory script is attached to and an enemy object the BasicEnemy is attached to. The plan is that when the player enters the trigger the territory script will tell it’s attached enemy to move towards the player, and a separate script will allow it to attack. If the player leaves the territory it will enter a rest state and do nothing. My two problems with this are that when I start the game the enemy makes a beeline for my player, even though the player is nowhere near the territory. The second is that the enemy is not restricted to only moving on the X axis, even though I have a rigidbody attached with Y and Z frozen. Any help would be greatly appreciated!

public class EnemyTerritory : MonoBehaviour
{
		public BoxCollider territory;
		GameObject player;
		bool playerInTerritory;

		public GameObject enemy;
		BasicEnemy basicenemy;

		// Use this for initialization
		void Start ()
		{
			player = GameObject.FindGameObjectWithTag ("Player");
			basicenemy = enemy.GetComponent <BasicEnemy> ();
			playerInTerritory = false;
		}
	
		// Update is called once per frame
		void Update ()
		{
			if (playerInTerritory = true)
			{
				basicenemy.MoveToPlayer ();
			}

			if (playerInTerritory = false)
			{
				basicenemy.Rest ();
			}
		}

		void OnTriggerEnter (Collider other)
		{
			if (other.gameObject == player)
			{
				playerInTerritory = true;
			}
		}
	
		void OnTriggerExit (Collider other)
		{
			if (other.gameObject == player) 
			{
					playerInTerritory = false;
			}
		}
}

public class BasicEnemy : MonoBehaviour
{
		public Transform target;
		public float speed = 3f;
		public float attack1Range = 1f;
		public int attack1Damage = 1;
		public float timeBetweenAttacks;


		// Use this for initialization
		void Start ()
		{
			Rest ();
		}
	
		// Update is called once per frame
		void Update ()
		{
			
		}

		public void MoveToPlayer ()
		{
			//rotate to look at player
			transform.LookAt (target.position);
			transform.Rotate (new Vector3 (0, -90, 0), Space.Self);
		
			//move towards player
			if (Vector3.Distance (transform.position, target.position) > attack1Range) 
			{
					transform.Translate (new Vector3 (speed * Time.deltaTime, 0, 0));
			}
		}

		public void Rest ()
		{

		}
}

if (playerInTerritory = true)
{
basicenemy.MoveToPlayer ();
}

should be

if (playerInTerritory == true)
{
      basicenemy.MoveToPlayer ();
}

You are setting playerInTerritory to true there.

using System.Collections;
using System.Collections.Generic;

using UnityEngine.UI;
using UnityEngine;

public class AiofFatZombies : MonoBehaviour {
Transform player;
public Animator anim;
public float playerHealth = 100;
public Text playerhath;
UnityEngine.AI.NavMeshAgent nav;
public Texture blooody;
public bool allowtohit=false;
float navspeed=0.5f;
public static float floathealth= 100;
public bool zombiesdeath = false;

 void Awake()
{
    if (!zombiesdeath)
    {
        nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
        //  player = GameObject.FindGameObjectWithTag("Player").transform;
        player = GameObject.FindGameObjectWithTag("Player").transform;
        anim = GetComponent<Animator>();

        playerhath.text = "Player Health =" + floathealth.ToString();

    }

}
 void OnGUI()
{ 
    nav.SetDestination(player.position);
    float distance = Vector3.Distance(player.transform.position, this.transform.position);
   
        navspeed += Time.deltaTime/50;

        nav.speed = navspeed;

        anim.SetBool("walk", true);
        anim.SetBool("attack",false);

        if (distance <=7)
        {
        nav.Stop();

            anim.SetBool("attack", true);
   floathealth -= Time.deltaTime;
        playerhath.text = "Player Health=  " +floathealth.ToString();
        if((int)floathealth==0)
        {
            hero.sendmassage = 1;

        }

        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), blooody);
  
            anim.SetBool("walk", false);
            
        
        }
   
        
    }


 public void ApplyDamage(float damage)

{
    playerHealth -= damage;
    if(playerHealth==0)
    {
    
        Death();

    }

}
 public void Death()
{
    zombiesdeath = true;
    nav.Stop();
    anim.SetTrigger("death");
    Destroy(this.gameObject, 10.0f);
}

}

what do I attach this to