- Home /
What is wrong with this melee system?
Hello, guys! I am following some tutorials on Unity and I can't seem to figure out what might be the problem with this melee system.
I have a player and an enemy.
Player script: #pragma strict
var Damage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
var TheMace : Transform;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
TheMace.animation.Play("Attack");
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if(Distance < MaxDistance)
{
hit.transform.SendMessage("Apply Damage", Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
Enemy script: #pragma strict
var Health = 100;
function ApplyDamage (Damage : int)
{
Health -= Damage;
}
function Update ()
{
if (Health <=0)
{
Dead();
}
}
function Dead ()
{
Destroy (gameObject);
}
The enemy doesn't die, what can I do?
Answer by Narv · Jul 17, 2013 at 10:41 PM
If you are sending a message to ApplyDamage, you should write in the sendmessage: "ApplyDamage" instead of "Apply Damage".
Also, try putting in Debug.log(""); statements to see which parts of the code are firing.
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object Raycast...? 1 Answer
Yet another NullReferenceException question. 2 Answers
Trouble with Ray & Raycasting and AddForce() 1 Answer
Trying to cast a ray towards an imaginary wall and get a "hit" / "end" point 2 Answers
Raycast only working once 2 Answers