Array of colliders and OnTrigger messages?

From what I’ve read in these threads and the documentation, it seems to me that if you want to use messages that are sent from colliders (OnTrigger…), you need to have a script attached to each game object that has a collider. Is that true? It seems a little messy.

(The alternative that I would prefer is to use an array of colliders, in a single script, on a parent object, and check for triggering for the colliders in the array.)

http://forum.unity3d.com/viewtopic.php?t=29419
http://forum.unity3d.com/viewtopic.php?t=28153

The trigger message is only sent to the object with the trigger collider directly attached, yes. If you had four trigger colliders as children with a parent object that had a script, you’d never get an OnTrigger event called.

Thank goodness this doesn’t apply for non-trigger children colliders.

In C# you can solve this by attaching a tiny script ( TriggerCallback.cs) to each of your triggering objects, which calls a definable function in your central gamehandling code. Using a delegate this is quite easy. You only have to attach the script to the Triggers and in your main code assign a function (same Type!) to the callback delegate.

Since i’m new to C# scripting and relatively new to Unity itself, there might be place for improvement. Please post if someone sees a flaw in my script or idea ;-). But besides of that i used this method recently and it worked quite well for me…

Pseudocode - assume cbscript holds reference to the attached “TriggerCallback.cs”

cbscript.OnTriggerCB = myMasterTriggerHandler()

void myMasterTriggerHandler( Gameobject whoWasTriggered , Collider whoTriggered )
{
   ....Do Somtething important
}

TriggerCallback.cs - attach to every trigger object

using UnityEngine;
using System.Collections;

public class TriggerCallback : MonoBehaviour 
{
	public delegate void OnTriggerCallbackType( GameObject iGotHit , Collider iTriggered );
	private OnTriggerCallbackType OnTriggerFunc;
		
 	public OnTriggerCallbackType OnTriggerCB
 	{
 		set
 		{
 			OnTriggerFunc= value;
 		} 		
 	}	 		
	
	void OnTriggerEnter( Collider trigger  )
	{
		if( OnTriggerFunc!= null) OnTriggerFunc( gameObject , trigger );			
	}
}

Very interesting, I was thinking delegates could be helpful in trigger/collision handling.
However, my C# and Unity knowledge aren’t really up to this.
Did you get this working ?
Anybody else who can help out with some working code ?

Thanks in advance, Patrick

Or you could use GameObject.SendMessageUpwards and set a variable in the parent. Maybe more costly

what are main uses of delegate ?