Melle script, enemy destroyed without mouse command once in range.

so everything was working till i realized once i got in range of an enemy it got destroyed, i think this has something to do with the fact that i may not have it set up so mouse 1 apply’s damage and instead what happens is it sends out a message apply damage once a frame once i get within 1.5 units of the enemy.

here is the Melle script:

#pragma strict

var Damage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;

function Update ()
{
	if (Input.GetButtonDown("Fire1"))
	
		var Hit : RaycastHit;
		if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Hit))
		
			Distance=Hit.distance;
			if (Distance < MaxDistance)
			
				(
				Hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver)
				);
}

and here is the script attached the enemy:

#pragma strict

var Health = 100;

function Update ()
{
	if (Health <= 0)
		{
		Dead();
		}
}

function ApplyDamage (Damage : int)
{
Health -= Damage;
}

function Dead()
{
	Destroy (gameObject);
}

As well as my ‘Fire 1’ settings

[31811-screenshot+2014-08-31+16.59.52.png|31811]

can someone help please?

Try this:

var Damage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;

function Update ()
{
	if (Input.GetButtonDown("Fire1"))
	{
		var Hit : RaycastHit;
		
		if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Hit))
		{
			Distance = Hit.distance;

			if (Distance < MaxDistance)
			{
				Hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
			}
		}
	}
}