enemy detect player then attack - c#

Hi,

In my game I want it so when the player enters the enemy raycast it goes towards him and attacks.

However I’m getting Error CS1503, the console says Assets/scripts/EnemyScript.cs(20,23): error CS1503: Argument #2' cannot convert object’ expression to type UnityEngine.Vector3' and error CS1502 he best overloaded method match for UnityEngine.Debug.DrawLine(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.Color)’ has some invalid arguments

I also get error CS0019 it says Operator -' cannot be applied to operands of type UnityEngine.Vector3’ and `float’

using UnityEngine;
using System.Collections;

public class EnemyScript : MonoBehaviour {

	bool seenPlayer = false;
	RaycastHit2D seePlayer;

	Animator anim;


	void Start()
	{
		anim = GetComponent<Animator> ();
	}

	void Update()
	{
		//Just a debug visual representation of the Linecast, can only see this in scene view! Doesn't actually do anything!
		Debug.DrawLine(transform.position, (transform.position - 10f), Color.magenta);

		//Using linecast which takes (start point, end point, layermask) so we can make it only detect objects with specified layers
		//its wrapped in an if statement, so that while the tip of the Linecast is touching an object with layer, the code inside executes
		if(Physics2D.Linecast(transform.position, (transform.position - 10f), 1 << LayerMask.NameToLayer("GameObject")))
		{
			//store the collider object the Linecast hit so that we can do something with that specific object
			//each time the linecast touches a new object with layer
			seePlayer = Physics2D.Linecast(transform.position, (transform.position - 10f), 1 << LayerMask.NameToLayer("Guard")); 
			seenPlayer = true; //since the linecase is touching the guard and we are in range, we can now interact!
		}
		else
		{
			seenPlayer = false; //if the linecast is not touching
		}
	}
}

^ my script

The problem in your code is wherever you have used (transform.position - 10f). transform.position is a Vector3 and 10f is a float. If say you want to go 10 units behind then you should use transform.position - transform.forward * 10. This way you can be sure it will always be 10 meters directly behind the transform. If forward then transform.position + transform.forward * 10. You can also replace 10 with a variable of your choice in the Inspector.