My animation doesn't damage enemies.

I’m a total newb. I’ve been using a series of these AWESOME tutorials by brackeys (http://www.youtube.com/watch?v=C6c9J9CSEvA)
I’ve linked the episode that i got stuck on.

This is the script :


#pragma strict

var TheDammage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
var TheSystem : Transform;

function Update ()
{
if (Input.GetButtonDown(“Fire1”))
{
//Attack animation
animation.Play(“attack”);

}
//if (TheSword.animation.isPlaying == false)
//{
//TheSword.animation.CrossFade(“idle”);
//}
//if (Input.GetKey (KeyCode.LeftShift))
//{
//TheSword.animation.CrossFade(“sprint”);
//}
//if (Input.GetKeyUp(KeyCode.LeftShift))
//{
//TheSword.animation.CrossFade(“idle”);
//}
}

function AttackDammage ()
{
//Attack function
var hit : RaycastHit;
if (Physics.Raycast (TheSystem.transform.position, TheSystem.transform.TransformDirection (Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage(“ApplyDammage”, TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}


I’ve got an animation that, at some point of it playing, is supposed to apply damage. The thing is, when it plays, nothing happens. I get an error saying “there is no animation attached to the melee game object”.

Do u have an animation component on the same object as the script?

If your error is before the raycast then the raycast may not get executed. So it may work once you get the error fixed.

Um, sorry for being a total derp, but please, speak more noob-friendly. What I can say is that I have script on both “melee” which is a non-existent object, and on “Sword” which is… a sword. The animations I have on the sword only.

What does the ApplyDammage function look like?
Show us that class

Also, change

if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}

to

if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
Debug.log("Hit");
}

Now try to hit the target. Then click window (at the top), then choose Console. Is there a message there saying “Hit”?
That will tell us if it is even getting into the function that sends the message.

I dunno what that is, as I said, a total noob is what i am.
Somehow, after one day, it happens to actually hit (and kill) the target, but the error persists. What happens now is that there is no idle animation, and there is no sprint animation. The attack one works just fine.

the SendMessage function (Unity - Scripting API: GameObject.SendMessage) calls a function on another object. The question is “can we see the code for ‘ApplyDammage’?” which should be in a script attached to whatever object you are attacking.

ps, is there any reason you’re spelling it dammage and not damage?

Okay, the script looks like this :
#pragma strict

var Health = 100;

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

function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
}

function Dead()
{
Destroy (gameObject);
}

And there is another one like this :
#pragma strict

var Health = 100;

function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;

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

function Dead()
{
Destroy (gameObject);
}
P.S.
the reason I spell it Dammage is because the guy from the tutorials spelled it that way, and I wanted to have it more or less the same not to screw up later.