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?
EDIT
It does indeed. Here’s the receipt script attached to the Enemy objects:
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.
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
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).