Using CSharpMessenger - Cleanly tying into collision events.

I’m using a messaging system and building an event driven framework for my current project. I really enjoy the cleanliness of the interfaces that an event driven system allows but I have a hurdle I’d like to hash out with others.

I have a mob manager object that takes care of instancing prefabs for the individuals mobs as they spawn. Inside each mob is an OnTriggerEnter(Collider otherobject) handler that is built into the attached collider component.

I’d like to get away from using the built in SendMessage functionality but I’m having a hard time thinking of a clean way to refer to a single GameObject using Broadcast.

As an example. Given a projectile gameobject and a badguy gameobject:

The projectile holds a damage variable and the badguy holds a health variable. On Collision I’d like to reduce the bad guys health by the projectiles damage variable.

I know I can use otherobject.transform.parent.SendMessage, but I’m trying to effect the same outcome without using the SendMessage functionality.

Any ideas, even if they’re just brainstorming would be helpful.

Don’t you want to try

collision.gameObject.GetComponent<ScriptReduceLife>().ReduceLife();

Instead of SendMessage.

That’s the other approach but then one object is tightly coupled to the other.

Also, just looking at the name of your script component you’re theoretically adding, my components are no where near that granular. I have a “GooSpider” script that has a function “resolveDamage”. Not a single component that I attach to a game object that does this. Is that much componentization common?

If you’ve more than one script that waiting for this event, yes, SendMessage will be better, in a way to optimize performances.
Why do you want to use something else than SendMessage ?

Performance concerns, which sounds silly just typing it but I’ve read time and again that SendMessage falls down on IPhone.

I appreciate your help. You have me thinking about how to split my scripts into more functional objects rather than the logical object organization I’m using. I still haven’t crossed the divide on object oriented versus component oriented structure yet.

I’m trying to observe good program structure so my project doesn’t collapse on itself down the road.

You can use inheritance. Have a DamageableMonoBehaviour or some such class that inherits MonoBehaviour and adds the health functionality, or only a virtual method. Then have the scripts of your squishy things inherit that. You can then use GetComponent() in your trigger, and call the appropriate method if the component exists. You can use layers to fine-tune what triggers what too.