Trouble with raycast hitting a tagged object

I am trying to apply damage to an enemy(zombie) when it enters the attack range of a tower.
Firstly I have set it up so the raycast works and only goes a certain amount of range, when the zombie enters the range, the tower looks at it.
But now I am trying to kill the zombie.

Sorry if this code is sloppy, I haven’t been doing this for long.
As you can see in the function attack(), I am trying to make the ray detect what it is shooting with a tag, but the Debug.log is not showing up when it is in range.

var distance;
var targetTransform : Transform;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
var fireRate = 0.4;
var allowedShot : boolean = true;
var damage = 100;
var hit : RaycastHit;


function Update ()
{
	targetTransform = GameObject.FindWithTag("Zombie").transform;

	
	
	
	distance = Vector3.Distance(targetTransform.position, transform.position);
	
	if (distance < attackRange)
	{
		
		attack();
	}
	
	if (distance > attackRange)
	{
		
	}
	
	Debug.DrawRay(transform.position, transform.forward * attackRange, Color.red);
	
	
	
}

function attack()
{
	var rotation = Quaternion.LookRotation(targetTransform.position - transform.position);
	transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
	forward = transform.TransformDirection(Vector3.forward);
	if (Physics.Raycast(transform.position, forward, hit, attackRange) && allowedShot)
	{
		if (hit.collider.gameObject.tag == ("Zombie"))
		{
			Debug.Log("zombie hit");
			//hit.transform.gameObject.SendMessage("ApplyDamage", 100, SendMessageOptions.DontRequireReceiver);
			allowedShot = false;
			reload();
		}
	}
}

function reload()
{
	
	yield WaitForSeconds(fireRate);
	allowedShot = true;

}

I have got it to work by removing the “if (hit.collider.gameObject.tag == (“Zombie”))” statement, but that is not what I want as the tower then begins to destroy anything in its path.

function Update ()
{
targetTransform = GameObject.FindWithTag(“Zombie”).transform;

This could potentially find a new target each frame.

Also, in your Raycast

Physics.Raycast(transform.position, forward, hit, attackRange)

try a more complete expression of ‘forward’, like Vector3.forward.

I think there is a mistake when you assign the value to “forward”. Vector3.forward is the world forward Vector, not the object’s local forward.

Try this:

var forward : Vector3 = transform.TransformDirection(transform.forward);