Need help with a C# A.I. script

So the last time I was on here I was asking for help with fixing a A.I. script being done in Javascript. Well, I’m back asking for some help, this time with an A.I. script done in C#. The problem is the same as the last script, which is when I apply the script to an enemy, the enemy rotates whenever they attack my player, which is great for 3D, but I’m currently working on a 2D game, and the enemy rotating causes it to disappear onscreen. Here’s the code:

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
	public Transform target;
	public int moveSpeed;
	public int rotationSpeed;
	public int maxdistance;
	private Transform myTransform;
	void Awake(){
		myTransform = transform;
	void Start () {
		GameObject go = GameObject.FindGameObjectWithTag("Player");
		target = go.transform;
		maxdistance = 2;
	void Update () {
		Debug.DrawLine(target.position, myTransform.position,; 
		myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
		if(Vector3.Distance(target.position, myTransform.position) > maxdistance){
			//Move towards target
			myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

I think I’ve of narrowed it down to the fact that it’s using Quaternion.Slerp as well as Vector3 (which if I remember are for 3D), but every time I try to remove or change it, I get an error. Thanks in advance to anyone who helps me out.

Edit: In my haste to post this, I forgot to post the error that I get whenever I replace Quaternion.Slerp.

After removing the lines containing Quaternion.Slerp and Vector3 with this:

if(target.position.x >= myTransform.position.x)
			//The target is on the right so move to the right
			myTransform.position.x += moveSpeed * Time.deltaTime;
			//Flip the ai to face the player
			myTransform.localScale.x = 1;
		if(target.position.x < myTransform.position.x)
			//The target is on the left so move to the left
			myTransform.position.x -= moveSpeed * Time.deltaTime;
			//Flip the ai to face the player
			myTransform.localScale.x = -1;

The error I get is " error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position’. Consider storing the value in a temporary variable". Apologizes that I didn’t include that sooner. Thanks again for the help.

You may want to look into using Transform.Rotate method

It’s important to understand that you are sort of still working in 3D always. Objects transform is always a Vector 3.
But you only want to affect the x and y values.
Now transform.position.x (and y and z) is read only, the way I understand it, which is why you get the error.
So try this instead.

myTransform.position = new Vector3(myTransform.position.x + moveSpeed * Time.deltaTime, myTransform.position.y, myTransform.position.z);