Trouble with enemy attacking and decreasing health

Hey guys, i’m fairly new to scripting and currently have a AI script that notices when your close then follows when you get closer and stops chasing when you run away. However i am having trouble making it attack and decrease the health. Here are the the scripts, appreciate the help and i know this has been asked before, i spent the last hour trying to find a solution…

AI:

var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 1;

var TheDammage = 40;

private var attackTime : float;

var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;

function Start ()
{
attackTime = Time.time;
}

function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);

if (Distance < lookAtDistance)
{
lookAt();
}

if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}

if (Distance < attackRange)
{
attack();
}
else if (Distance < chaseRange)
{
chase ();
}
}

function lookAt ()
{
renderer.material.color = Color.yellow;
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}

function chase ()
{
renderer.material.color = Color.red;

moveDirection = transform.forward;
moveDirection *= moveSpeed;

moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);

if (Distance < attackRange Time.time > attackTime)
{
attackTime = Time.time + attackRepeatTime;
Debug.Log(“i’m attacking you”);
}
}

function attack ()
{
if(attackRange < 1.5)
{
renderer.material.color=Color.blue;
}
}

Health:

#pragma strict

public static var health : float = 100;

function OnGUI()
{
GUI.Label(Rect(Screen.width-200,Screen.height-50,200,20),"Player Health " + health);
}

Hey,
“It doesn’t work” is always a bad start for describing your problem. I suppose that, for example, your attack-function does get called?
I don’t see where you change / decrease health in your code. When attacking you only change the material color, but you never reduce the variable health.
And you should use code-Tags when posting code on the forums.

AI:

var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 1;

var TheDammage = 40;

private var attackTime : float;

var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;

function Start ()
{
	attackTime = Time.time;
}

function Update ()
{
	Distance = Vector3.Distance(Target.position, transform.position);
	
	if (Distance < lookAtDistance)
	{
		lookAt();
	}
	
	if (Distance > lookAtDistance)
	{
		renderer.material.color = Color.green;
	}
	
	if (Distance < attackRange)
	{
		attack();
	}
	else if (Distance < chaseRange)
	{
		chase ();
	}
}

function lookAt ()
{
	renderer.material.color = Color.yellow;
	var rotation = Quaternion.LookRotation(Target.position - transform.position);
	transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}

function chase ()
{
	renderer.material.color = Color.red;
	
	moveDirection = transform.forward;
	moveDirection *= moveSpeed;
	
	moveDirection.y -= gravity * Time.deltaTime;
	controller.Move(moveDirection * Time.deltaTime);
	
	if (Distance < attackRange  Time.time > attackTime)
		{
			attackTime = Time.time + attackRepeatTime;
			Debug.Log("i'm attacking you");
		}
}

function attack ()
{
    if(attackRange < 1.5)
    {
    renderer.material.color=Color.blue;
	}
}

Health:

#pragma strict

public static var health : float = 100;

function OnGUI()
{
	GUI.Label(Rect(Screen.width-200,Screen.height-50,200,20),"Player Health " + health);
}

I might take a look later