SendMessage function not working
Could anyone help me with this it has me stumped for days..
I am trying to deduct health from a second game object by using the sendmessage function.
MeleeSystem.js
#pragma strict
var TheDamage : int = 50;
var Distance : float;
var MaxDistance : float = 20;
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)
{
Debug.Log("11111");
hit.transform.gameObject.SendMessage("ddd", 50, SendMessageOptions.DontRequireReceiver);
hit.transform.gameObject.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
EnemyLogic.js
#pragma strict
var Health=100;
function Update(){
if(Health<=0){
Dead();
}
}
function ApplyDamage(theDamage: int){
//Health-=theDamage;
Debug.Log("22222");
Health-=50;
}
function Dead(){
Destroy(gameObject);
}
function ddd(ge : int){
Debug.Log("3333");
}
2222 and 33333 never print out so its definitely the SendMessage Function that's not working...
Print out your hit.transform.gameObject
to check if it's actually the object with EnemyLogic.js that gets the message. I remember you can use something like (syntax not sure) Debug.Log(hit.transform.gameObject, hit.transform.gameObject.transform) so that when you click on Console message it'll bring you directly to the object.
I did, it is referencing the "ground" object only. Doesn't seem to recognise my enemy object.
Answer by MRDJR97 · Nov 08, 2017 at 05:47 PM
I've figured it out, the raycast was firing at a weird angle (about 45 degrees) on the x axis, so it wasn't hitting my enemy object. Don't know why.
Your answer
Follow this Question
Related Questions
Apply Damage Universally 0 Answers
Damage not working properly 1 Answer
Continues Health Decrease problem after no collision 2 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers
Health bar help 1 Answer