SendMessage Has No Receiver Error

I would like to give enemies hitpoints, so basing my scripts off those found in the numerous tutorials I’ve come up with some scripts to do that…

However, whenever my projectile comes into contact with another rigidbody I receiver the error “SendMessage ApplyDamage has no receiver” even though I’ve placed a DamageReceiver script on the rigidbodies I’m shooting!!

Here are the scripts:

The Laser Gun:

var projectile : Rigidbody;
var speed = 20;
var reloadTime = 0.5;
private var lastShot = -10.0;


function Update() {
	
	if (Time.time > reloadTime + lastShot) {
	
		if (Input.GetButtonDown ("Fire3")) {		
			var clone : Rigidbody;
			clone = Instantiate(projectile, transform.position, transform.rotation);
			
			clone.velocity = transform.TransformDirection (Vector3.forward * speed);
			
			Physics.IgnoreCollision(clone.collider, transform.root.collider);
			
			lastShot = Time.time;
		}
	}
}

The Laser(Projectile):

function OnCollisionEnter () {
	collider.SendMessage("ApplyDamage");
	Destroy(gameObject);
}

The DamageReceiver:

var hitPoints = 1;

function ApplyDamage () {
	hitPoints = hitPoints - 1;
		
	if(hitPoints <= 0){
		Destroy(gameObject);		
	} 	
	
}

Use:

collider.SendMessage("ApplyDamage", null, SendMessageOptions.DontRequireReceiver);

It may be that your lasers are simply hitting other objects that don’t have the receiver, which is where the error comes from.

It shouldn’t be, I’ve placed a box right in front of the character that has a rigidbody, cube collider, and has the damagereceiver script. And the laser will hit the box, and apply the physics (knocks the box over) but gives me the error :frowning:

Edit
Implementing the code you provided gives the same error :frowning:

So in a continued effort to solve my problem I loaded back up my completed version of the 3D Platformer Tutorial. Which might have been a bad idea because now I seem to have even more problems.

I did a test run of the game to make sure the hit/apply damage functions were working…and to my surprise when the enemy robot tried to “Slam” Lerpz I received the message “SendMessage Slam has no receiver”. I’m surprised by this because all of the scripts in the 3D Platformer Tutorial have been unchanged!

Here is the line of the code that the error points me to:

target.SendMessage("Slam", transform.TransformDirection(slamDirection));

Edit:
On further inspection, the laser traps still work as they are suppose to, but the robot guards no longer deal damage or apply the Slam effect. :frowning:

Well, the only reason for this I can think of is that the part of the main object with the collider doesn’t have a script.

If that’s not the case, then you’re sending ApplyDamage without the right variables to call the function.

If THAT’S not the case, then you’ve got a bug and should send a bug report.

Edit: Oh! Caught the problem!

In the OnCollisionEnter script you should have it like this:

function OnCollisionEnter(hit : Collision) {
   hit.gameObject.SendMessage("ApplyDamage");
   Destroy(gameObject);
}

Well I fixed my Lerpz problem in the 3D Tutorial…I just deleted the object and made placed a new one and reapplied all the scripts…that seemed to work somehow.

However, in my 2D game I’m still getting the same error…I place a Crate prefab into the scene (which has a box collider and rigidbody applied to it already) then I put my DamageReceiver script on it. And nothing!

Maybe I’ll have to report it as a bug…I was just hoping it was some simple thing I was missing!

The receiver script is in the same GameObject, or is in higher hierarchy. Case in a higher object, have to use SendMessageUpwards instead.

The last post was Dec 10, 2008… No reason to respond to it and also, SendMessage is considered bad to use these days anyways.

1 Like