- Home /
Damage script not working?
Following this Guide: https://www.youtube.com/watch?v=FKT7jIW6ORU
Everything seems correct, but the enemy (capsule) simply doesn't lose health nor die. I've been at it for an hour or two and tried all I could, but nothing seems to be in my favor.
the two scripts are:
=====================================================
MeleeSystem Below
#pragma strict
var TheDamage : 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", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
================================================================================= EnemyLogic:
#pragma strict
var Health = 100;
function ApplyDamage (TheDamage : int)
{
Health -= TheDamage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
Destroy (gameObject);
}
EDIT: I made some tweaks the code(s) will be listed for paste bin (it takes up spaces) below. I think I have fixed some stuff, but now I have only one ERROR, it reads as follows.
Assets/EnemyLogic.js(7,12): BCE0034: Expressions in statements must only be executed for their side-effects.
Answer by Mads_AE · May 08, 2014 at 09:17 AM
I'm not that much into JavaScript, but from a C# perspective i would probably have the Function in the EnemyLogic public (is that a thing in JS), and i would get the component of the enemy script instead of sending a message, last thing is i would send the message to the game object and not the transform.
I hope one of those things help you out, i know way to well how it is to be stuck with something that seems easy and effortless.
Hi $$anonymous$$ads_AE, I tried what you suggested by changing it to the following line of code
public function ApplyDamage (TheDamage : int)
I don't get debug errors with the script, but I think it's something wrong with the EnemyLogic, because the $$anonymous$$eleeSystem still registers the distance whenever I click an object within specified distance of me. So, I believe it is either the EnemyLogic or the following lines of code in the $$anonymous$$eleeSystem Script
{
hit.transform.Send$$anonymous$$essage("ApplyDamage", TheDamage, Send$$anonymous$$essageOptions.DontRequireReceiver);
}
I've rewatched the guide many times over now and cannot spot any noticeable difference(s) (or any at all really between my code and his.
I think if you save the script you want to save to into a var, like this
var script : EnemyLogic; script = hit.gameObject.getComponent(EnemyLogic); script.ApplyDamage(TheDamage);
Im sorry if some of the camel casing is wrong as JS is not m