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.

–

Hey. I know this is dragging on but changing to chest.localRotation didn't entire fix it. Idk if you can tell in this photo but when looking up or down he still does that weird leaning thing.[136432-capture-4.jpg|136432]

–

1 Answer

1

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.

[136436-capture2-1.jpg|136436] Like in the first photo he was able to look directly to the ground, but only at certain angles. But after applying localrotation he can look down but it looks strange/sideways

–