BroadcastMessage to all object in an array

Evening, gents.

Right now, I’m trying to get some more done with combat. I’ve an array that lists all of the objects in the current scene that are tagged as Enemy, and now I want to have it send a message to all of those objects in said array.

I’m doing so with the following:

var enemies : GameObject[];

function Start () {
enemies = GameObject.FindGameObjectsWithTag("Enemy");

function SendDamage (Damage : int) {

	for(var Enemy in enemies)
		Enemy.BroadcastMessage("damageEnemy", Damage);


The array is created just fine, but when it gets to broadcasting the message, it says that it has no receiver. What am I doing wrong?

It does indeed. Here’s the receipt script attached to the Enemy objects:

function damageEnemy (Damage) {
	if(isInRange) {
		enemyHealth -= Damage;
		print("Hit for" + Damage + " Damage!");


Aren’t you calling your BroadcastMessage on your enemies, looking for a receiver elsewhere? Also, I was under the impression that BroadcastMessage went out to every instance of an object with a suitable listener, meaning that you don’t have to loop through your list.

If you take a short look into the docs you will see that BroadcastMessage has 3 parameters. The last two have a default value when they aren’t given. SendMessageOptions.DontRequireReceiver.

Also if you don’t really need BroadcastMessage you should use SendMessage instead. BroadcastMessage will send the message to ALL child objects of the given object.

All that sendmessage stuff is nice but i would always prefer an interface for such a task :wink:

I suspect you’ve just accidentally tagged something with “Enemy”. Add some debug code (eg. Debug.Log(Enemy,Enemy)). And I just have to say: variables with uppercase letters may be perfectly legit, but they freak me out (to my eyes, they are types).

Whelp, guess who’s embarrassed now? I seem to have forgotten to reset the hitboxes in another part of the attack code…

Oh, and it seems that Unity registers (Damage) and (Damage : int) as two different things, so that’s where the issue stemmed from.