Help with Damage script

Hello guyz,
I’m working on a survival game and got many help from youtube but i get problems,I have a scipt named “EnemyLogic” Before i added animation and the weapon ,The Enemy damage stopped working

Script code

#pragma strict

var Health : int = 100;

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

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

function Dead()
{
    Destroy (gameObject); 
}

Ummm… You might need to provide more information. What exactly stopped? The death? Taking damage? Compiler errors?

Nothing,Just it won’t let the enemies health go down :cry:

What calls ApplyDamage? Is that still being called? Add a Debug.Log call in ApplyDamage to check.

Should i replace “ApplyDamage” to "Debug.Log "???

No, Change ApplyDamage to:

function ApplyDamage (TheDamage : int)
{
Debug.Log("Hello from ApplyDamage"); 
Health -= TheDamage;
}

Then run your game, attack stuff and check the unity console. There should be some hello messages there.

Nope,Nothing at all.

And im using JAVASCRIPT

No messages in the console? That means that method is never being called. Please show me the code that calls the ApplyDamage function.

#pragma strict

var Health : int = 100;


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

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

function Dead()
{
    Destroy (gameObject); 
}

ApplyDAMAGE Code

#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("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
 }
   }
      }
         }

At line 21

After line 18 add:

Debug.Log(“I HIT AN ENEMY”);

The run and play and check the console again.

Yeah

I HIT AN ENEMY
UnityEngine.Debug:Log(Object)
EnemyLogic:Main() (at Assets/EnemyLogic.js:18)

now?

ok, next add this line after line 20:
Debug.Log(“HE WAS IN RANGE”);

Not working

But When i replace “Debug.Log(“I HIT AN ENEMY”);” with “Debug.Log(“HE WAS IN RANGE”);”

it works

Thats your problem. The range is too short. Set MaxDistance to 10 and try again.

But When i replace “Debug.Log(“I HIT AN ENEMY”);” with “Debug.Log(“HE WAS IN RANGE”);”

it works

Change

var MaxDistance : float = 1.5;

to

var MaxDistance : float = 10;

Your problem is that MaxDistance is too low