- Home /
Broadcast to a script question
this is what I'm doing.
script 1{
public TargetScript target;
void Hit() { target.BroadcastMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver); } } Now, this works when I have ApplyDamage() under TargetScript. I have another script, also attached to the same object TargetScript is attached to. I want to receive the broadcast on the other script.
I tried target.gameObject.BroadcastMessage hoping it would get the gameObject target is attached to, then broadcast to all scripts attached to the gameObject, but this did not work.
Any suggestions as to how I would do this?
Answer by hathol · Aug 07, 2012 at 03:21 AM
Are you sure you have the ApplyDamage function in your other script as well (no typo or something)? Just tried it here and broadcast message to a target GameObject/Script (both) is received by all attached scripts that have the function.
Answer by bmannion4 · Aug 07, 2012 at 04:26 AM
I figured out my issue. Managed to forget to attach the script the the prefab... so it obviously would not get called. Lack of coffee error.
Answer by aldonaletto · Aug 07, 2012 at 03:35 AM
It should work: target.BroadcastMessage("ApplyDamage", damage, ...) calls the function ApplyDamage(damage) in the target script and in any other script attached to the same object or to any of its children. As @hathol said, check if the ApplyDamage exists in the other script, and if its name has no typos.